Problem click button python selenium

-2

I'm having trouble clicking a button, so try

gives the ElementNotVisibleException error: Message: element not interactable

pdf = driver.find_element_by_xpath('//*[@class="btn btn-default btn-segunda-via-aberta ng-scope"]')
pdf.click()

    
asked by anonymous 17.10.2018 / 20:37

1 answer

0

It seems that when you try to click on the object, it does not yet 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 it reaches the point where it tries to click on an element that has not yet been displayed, as the browser takes a little longer to process the page that has been loaded.

To mitigate / resolve this issue, it is recommended to use one of the Waits (%)% (documented waits ) that is most appropriate for you. So the element will only be accessed after it has been created / made available. It is possible to create your own Selenium conditions, but according to the documentation, there is already a Wait condition that seems to fit 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: Your visibility_of_element_located might be 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 the xpath.

Failing this, it may be a more annoying problem to circumvent: Web Driver attempts to simulate a user's actions, but there are often differences in actual usage, whether due to incorrectly calculated elements or incorrectly calculated Difference of typing time / mouse movements. While a real user can navigate smoothly, WebDriver-generated events can not find the elements correctly.

In such cases, the solution is to replace native calls to the Selenium API by "injecting" direct JavaScript commands into the browser that perform the desired actions. Instead of xpath use:

driver.execute_script("arguments[0].click();", pdf)
    
17.10.2018 / 22:20