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.