Home > OS >  Unable to find element by xpath using selenium
Unable to find element by xpath using selenium

Time:08-17

I'm very new to Python and I'm trying to follow along with a video on web scraping with Selenium. In the enter image description here

However, when I am trying to do this, I have only two options (as seen below). Attempting to just use the same code to find the element by it's xpath simply doesn't work. I get an error that says

AttributeError: 'WebDriver' object has no attribute 'find_elements_by_xpath'

What am I doing wrong here? Do I need to do something to be able to access these?

enter image description here

CodePudding user response:

If you have downloaded the Selenium recently (I mean, not a year ago or so), the you are using the latest version of Selenium (Selenium 4), and there is a change in Selenium 4 where you use a By class to get to the locator strategy.

from selenium.webdriver.common.by import By

driver.find_element(By.ID, "element path")
driver.find_element(By.XPATH, "element path")
driver.find_element(By.CSS_SELECTOR, "element path")
driver.find_element(By.CLASS_NAME, "element path")

etc.

Same goes for find_elements

driver.find_elements(By.ID, "element path")
driver.find_elements(By.XPATH, "element path")
driver.find_elements(By.CSS_SELECTOR, "element path")
driver.find_elements(By.CLASS_NAME, "element path")

The key here is to import the By class and use it.

find_element_by_xpath and find_elements_by_xpath etc are deprecated in Selenium 4.

CodePudding user response:

In the latest Selenium versions driver.find_element_by_xpath method was changed by driver.find_elements(By.XPATH,.
So, instead of the old syntax like

driver.find_element_by_xpath("//*[@id='Username']")

Now you should use the new syntax like

driver.find_element(By.XPATH, "//*[@id='Username']")

The same about all the other old find_element_by_* and find_elements_by_* methods.

  • Related