程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Selenium+python web automation, about no such element: error handling

編輯:Python

When dealing with the drop-down box that appears after suspension , I have encountered the following two kinds of errors

One no such element:Unable to locate element: {“method”:“xpath”,“selector”:"//div[@class=‘s-user-setting-pfmenu’]/a[text()=‘ Advanced search ’]"}

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved