Problem click python botan Selenium

Asked

Viewed 2,007 times

-1

I’m having trouble clicking a button, I tried it this way

gives Elementnotvisibleexception: Message: element not interactable error

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

inserir a descrição da imagem aqui

1 answer

0


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)
  • Thanks for the answer, but the element is already on the screen when I try to click on it, on the page this is the only element with this xpath, I tried to put your solution in the code and returned the same error

  • @Guilherme added another possible solution

  • guy worked out now, thank you so much

  • Sorry to abuse, but I need to get 2nd route of invoices on the site, until then beauty your solution worked, problem now is that appeared the invoice next month, with another boot exactly the same, giving an inspect,?

  • @Try to use the find_elements_by_xpath, with elements plural. It returns a list of all elements that match xpath. Here you go to the list and choose: pdf[0] or pdf[1] etc.

  • Show, thank you so much, saved my life

  • @Choose if the answer answered, mark as accepted answer, to close the question

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.