When dealing with the drop-down box that appears after suspension , I have encountered the following two kinds of errors
In baidu home page , Hover the mouse over " Set up ", Then move to " Advanced search " And click the
Error code :
# wait for " Set up " Button appears
WebDriverWait(dr,30).until(EC.visibility_of_element_located((By.XPATH,"//span[text()=' Set up ']")))
# use ActionChains The form of the chain , Move the mouse to " Set up ", And then click " Advanced search "
ac = ActionChains(dr)
ac.move_to_element(dr.find_element_by_xpath("//span[text()=' Set up ']")).\
click(dr.find_element_by_xpath("//div[@class='s-user-setting-pfmenu']/a[text()=' Advanced search ']")).perform()
# click() Medium dr Report errors
time.sleep(5)
dr.quit()
Execution results
I'll follow ActionChains Step by step modification of , After modification, it still doesn't work
Finally, in turn perform move_to_element/click, Successful implementation
The modified code
dr = webdriver.Edge()
dr.get("http://www.baidu.com")
dr.maximize_window()
WebDriverWait(dr,30).until(EC.visibility_of_element_located((By.XPATH,"//span[text()=' Set up ']")))
ac = ActionChains(dr)
ac.move_to_element(dr.find_element_by_xpath("//span[text()=' Set up ']")).perform()
ac.click(dr.find_element_by_xpath("//div[@class='s-user-setting-pfmenu']/a[text()=' Advanced search ']")).perform()
time.sleep(5)
dr.quit()
stay 58 Same city - Brand apartment page (https://sjz.58.com/pinpaigongyu/), Hover the mouse over " Location "-“ Area ”-“ changan ”
# stay " Brand apartments " page , Scroll the target property to the area
element_wz = driver.find_element_by_xpath('//div[@class="selectBar select-item"]//li[@class="wz"]')
action = ActionChains(driver)
action.move_to_element(element_wz).perform()
# WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, '//div[@class="selectBar select-item"]//a[@listname="changan"]')))
element_1 = driver.find_element_by_xpath(
'//div[@class="selectBar select-item"]//div[@class="wz-mod1"]/a[@class="select"]')
action.move_to_element(element_1).perform()
# The following line find Report errors
action.move_to_element\
(driver.find_element_by_xpath('//div[@class="selectBar select-item"]//a[@listname="changan"]')).perform()
Report errors :stale element reference: element is not attached to the page document( Element not loaded on page )
element_wz = driver.find_element_by_xpath('//div[@class="selectBar select-item"]//li[@class="wz"]')
action = ActionChains(driver)
action.move_to_element(element_wz).perform()
element_1 = driver.find_element_by_xpath('//div[@class="selectBar select-item"]//div[@class="wz-mod1"]/a[@class="select"]')
action.move_to_element(element_1).perform()
# 1. Move the mouse to " Area " after , The page has been refreshed ." changan " The label cannot be located .
action_new = ActionChains(driver)
# 2. Use the refreshed driver establish ActionChains example , You can continue to locate
time.sleep(5)
element_2 = driver.find_element_by_xpath('//div[@class="selectBar select-item"]//a[@listname="changan"]')
action_new.move_to_element(element_2).perform()