[Solved] Python Selenium Error: AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_xpath‘

Python selenium Error:

el = driver.find_element_by_xpath('//*[@id="changeCityBox"]/ul/li[2]/a')
driver.find_element_by_xpath('//*[@id="search_input"]').send_keys('python',Keys.ENTER)

Solution: Modify the codes above to:

from selenium.webdriver.common.by import By
el = driver.find_element(By.XPATH,r'//*[@id="changeCityBox"]/ul/li[2]/a')
driver.find_element(By.XPATH,r'//*[@id="search_input"]').send_keys("python",Keys.ENTER)

War here to use the browser driver for Google, other browsers can also be Edge to edge, modify the driver needs to configure the environment

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys   # Button commands for the keyboard
from selenium.webdriver.common.by import By
driver = Chrome()
driver.get("https://www.lagou.com/")
# Find the element copyxpath that the browser needs to operate on
el = driver.find_element(By.XPATH,r'//*[@id="changeCityBox"]/ul/li[2]/a')
# el = driver.find_element_by_xpath('//*[@id="changeCityBox"]/ul/li[2]/a')
el.click() # Click event
# find the input box F12 element copyxpath, enter python content, enter or search button xpath
driver.find_element(By.XPATH,r'//*[@id="search_input"]').send_keys("python",Keys.ENTER)
# driver.find_element_by_xpath('//*[@id="search_input"]').send_keys('python',Keys.ENTER)

Read More: