Tag Archives: Python Selenium error

[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)

Python Selenium: element is not attached to the page document error

Recently, when I was working on an automatic office project in selenium, I encountered an error in the mouse event Click(), when I was looking for page elements

div = driver.find_elements_by_xpath('//*[@id="test"]') #Find certain elements of a page
for x in range(10):#click on the first 10 links in order
	div[x].click()
	driver.switch_to.window(driver.window_handles[2])#switch to the page handle of the clicked page to perform the operation
	#Omit the operation code here
	driver.close()#close the current tab
	driver.switch_to.window(driver.window_handles[1])#switch to the initial tab handle

When the above code is executed, you can click the first link. When you loop to the second link, you will get the error of element is not attached to the page document.
After careful observation, it is found that when the first link is closed, the initial page will be forced to refresh once. Therefore, it is very likely that the element in the div has changed, resulting in that the element cannot be found later. Therefore, I try to put the statement of finding the element in the loop, that is, each loop will look up the element again, and the problem is solved. After the solution, the code comparison is as follows:

for x in range(10):#Click on the first 10 links in order
	div = driver.find_elements_by_xpath('//*[@id="test"]') # Move the find element statement inside the loop
	div[x].click()
	driver.switch_to.window(driver.window_handles[2])#Switch to the page handle of the clicked page to perform the operation
	#Omit the operation code here
	driver.close()#close the current tab
	driver.switch_to.window(driver.window_handles[1])#switch to the initial tab handle