-1
It seems that when you try to click on the object, it still does not exist on the screen, it is not visible or clickable at the time.
Dynamic pages often create the elements before enabling them, or create hidden elements to then display them. When we are using Selenium it is common for the script to run too fast, and get to the point where it tries to click on an element that has not yet been displayed, because the browser takes a little longer to process the page that has been loaded.
To mitigate/solve this problem it is recommended to use one of the waits (Waits
) of Selenium
(documented here) which is most appropriate for your case. Thus the element will be accessed only after it has been created/made available. It is possible to create your own conditions of Wait
, but according to the documentation, there is already ready the condition visibility_of_element_located
which seems to be appropriate to your problem.
pdf = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.XPATH, '//*[@class="btn btn-default btn-segunda-via-aberta ng-scope"]'))
)
pdf.click()
Another possible cause: it may be that your xpath
is finding more than one element with these criteria. If this is the case, Selenium returns only the first element, which may not be what you want to click. Fix xpath.
Failing this, it can be a more annoying problem to circumvent: The Web Driver tries to simulate a user’s actions, but there are almost always differences in relation to actual use, whether it is due to the positioning of elements that are calculated incorrectly or due to the time difference of the typing/mouse movements. While a real user can navigate smoothly, events generated by the Webdriver cannot find the elements correctly.
In such cases, the solution is to replace native calls to the Selenium API by "injecting" Javascript commands directly into the browser that perform the desired actions. Instead of pdf.click()
use:
driver.execute_script("arguments[0].click();", pdf)
what error occurs?
– Lucas Miranda