Python/Selenium - Webscrapping, click

Asked

Viewed 102 times

0

Hello, I started to study Python a little bit and I’m doing some tests with Lenium and I came across an error in which I am not able to solve. I’m trying to click a button on a website but the following error appears:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/div[2]/div[1]/div[1]/div/a"}
  (Session info: chrome=87.0.4280.88)

Follows code below:

from selenium import webdriver

chromedriver = "chromedriver"
driver = webdriver.Chrome(chromedriver)
url = '/'
driver.get(url)

driver.find_element_by_xpath("/html/body/div[3]/div[2]/div[1]/div[1]/div/a").click()
  • 1

    the problem is the xpath, not the click. How did you get this xpath? Are you sure it’s right?

  • I copied it straight from the inspection.

  • Since Lucas said the problem is not the click but the xpath. Could you print out which element of the page you’re trying to get the xpath? Hug!

  • best use find_element_by_id. Is it possible in this case?

1 answer

1

This is an Exception played by Selenium when it cannot find in the element.

In your case, you cannot find the element as there is no div[2] inside div[3].

This kind of xpath is not very good to find the elements, try using the existing expressions of xpath to get a better result.

Browser other questions tagged

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