[英]Is there any way to give limit time for Selenium to find element/s?
我正在嘗試使用Python Selenium Chrome驅動程序自動化Google地圖,我的代碼可以穩定地獲得我需要的內容, 但不是性能時間。
我使用迭代代碼通過谷歌地圖獲取商家數據,使用Selenium獲得1個商家數據的時間速率低於1秒,但是當代碼捕獲異常時,它將花費很長時間,如6-7秒。
以下是執行時間的一些摘要:
https://picoolio.net/image/dgxC (正常執行時間)
https://picoolio.net/image/dgxT (開放時間發生異常時的執行時間,太長)
https://picoolio.net/image/dgxV (發生異常時的執行時間,有時按預期運行)
以下是獲取開放時間列表的一些代碼:
try:
openhour = wait(driver, 1).until(EC.visibility_of_element_located((By.XPATH,
'.//span[@class="section-open-hours-button maps-sprite-pane-info-arrowup"]')))
driver.execute_script("arguments[0].scrollIntoView(true);", openhour)
openhour.click()
openhourstacked = wait(driver, 1).until(EC.visibility_of_element_located((By.CLASS_NAME,
'section-open-hours-container-hoverable'))).get_attribute("aria-label")
openhourlist = openhourstacked.split(",")
openhourlist[len(openhourlist) - 1] = openhourlist[len(openhourlist) - 1].split(".")
openhourlist[len(openhourlist) - 1] = openhourlist[len(openhourlist) - 1][0]
except NoSuchElementException:
openhourlist = []
print("No Open Hour list in this merchant!")
openhour_trig = True
except WebDriverException:
openhourlist = []
print("Failed to load Open Hour list in this merchant!")
openhour_trig = True
很多建議說要正確地進行顯式等待,我應該使用WebDriverWait和EC(預期條件)的組合,我直接使用上面的代碼嘗試它,但WebDriverWait似乎不適用於所有傳入的異常,換言之,有時會成功,有時會失敗。
我嘗試用EC.presence_of_all_element_located
替換EC.visibility_of_all_element_located
,但它沒有任何區別。
當發生任何異常時,我希望使執行時間仍然平穩,除了上述方法之外,有沒有辦法讓Selenium在超時的情況下停止查找元素? 或者這是一個互聯網連接問題? 歡迎任何想法!
更新:所以我將開放時間代碼修改為這樣的東西
try:
if len(driver.find_elements(By.XPATH, './/span[@class="section-open-hours-button maps-sprite-pane-info-arrowup"]'))>0:
openhour = wait(driver, 5).until(EC.visibility_of_element_located((By.XPATH,
'.//span[@class="section-open-hours-button maps-sprite-pane-info-arrowup"]')))
#openhour = driver.find_element_by_xpath('.//span[@class="section-open-hours-button maps-sprite-pane-info-arrowup"]')
driver.execute_script("arguments[0].scrollIntoView(true);", openhour)
openhour.click()
#openhourstacked = driver.find_element_by_class_name('section-open-hours-container-hoverable').get_attribute("aria-label")
openhourstacked = wait(driver, 5).until(EC.visibility_of_element_located((By.CLASS_NAME,
'section-open-hours-container-hoverable'))).get_attribute("aria-label")
openhourlist = openhourstacked.split(",")
openhourlist[len(openhourlist) - 1] = openhourlist[len(openhourlist) - 1].split(".")
openhourlist[len(openhourlist) - 1] = openhourlist[len(openhourlist) - 1][0]
else:
openhourlist = []
print("No Open Hour list in this merchant! (Not an Exception)")
except NoSuchElementException:
openhourlist = []
print("No Open Hour list in this merchant!")
openhour_trig = True
except WebDriverException:
openhourlist = []
print("Failed to load Open Hour list in this merchant!")
openhour_trig = True
是的,這種方式它確實避免了Exception,但這會像之前一樣重復搜索,因此當找不到元素時,它可能會再次長達6-7秒進行搜索..顯式等待對這些條款沒有多大幫助我認為
摘要:
在您的代碼中嘗試增加顯式等待的時間。 它不會減慢你的速度,但它會阻止這些例外。 嘗試5秒而不是1秒。 試試這個
wait(driver,5)
我們使用等待因為加載DOM需要時間,有時我們需要等待頁面/元素加載/隱藏/更改,我們必須根據更改行事。
顯式等待的作用是等待達到預期條件的給定時間,並且如果滿足條件則每500毫秒檢查一次。 這樣,在執行下一個任務之前,您將丟失最多500毫秒的時間。 如果條件未及時滿足,則會拋出超時異常。 顯式等待不會解決您的所有問題。
您仍然需要檢查可能的異常並確定發生時要執行的操作。 有些站點加載緩慢或有時帶寬較低。 那你就沒辦法加快這個過程。
您必須先分析頁面並確保預期結果。 如果您正在等待元素存在但有可能不存在,那么您應該相應地處理異常。
如果您正在檢查頁面中是否存在元素,那么最好不要等待該元素,但嘗試使用find_elements_
並檢查列表的大小。 這樣你就可以避免異常,而且會更快。
driver.find_elements(By.ID, "locator").size()>0
yes explicit wait(webdriver wait)解決了這個問題。另外簡單的方法是用timeseconds創建一個for循環並在循環中寫入find元素,所以即使它失敗也會檢查180secs並寫入超出預期等待時間的條件
for (int second = 0; second < 60; second++) {
try {}
catch (WebDriverException we)
{}
if (second >= 60) {
//write failure
}
我知道這已經是很長的問題並得到解決方案,忘了再次更新,但我想,我意識到將隱式等待設置為幾秒鍾,例如:2秒就像這個driver.implicitly_wait(2)
將有助於限制WebDriver搜索到最后一秒,超過最后一秒,它將拋出異常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.