简体   繁体   中英

Unable to find element by xpath using selenium

I'm very new to Python and I'm trying to follow along with a video on web scraping with Selenium. In the video , the person walking through how to do this has a whole bunch of different possible methods of finding an element on a web page. He is trying to identify a button on the page using an xpath. 在此处输入图像描述

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?

在此处输入图像描述

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM