我想讓Selenium等待,直到一個網頁被加載。我在 macOS 10.15.7 上使用 Safari 14.1.2
。所以我嘗試了以下方法:
//login process and it successes and the following page shows up
login_submit.click()
//授權后的頁面,讓selenium等待,直到``hoge``` 是可點擊的。
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'hoge'/span>))
這段代碼并沒有像預期的那樣作業。它以一個錯誤selenium.common.exceptions.NoSuchFrameException:失敗。該錯誤立即出現,似乎沒有10秒的等待。
堆疊跟蹤如下。(路徑已被修改。
Traceback (most recent call last):
檔案 "/login.py", line 99, in<module>
login_schedule().quit()
檔案 "/login.py", line 67, in login_schedule
wait.until(EC.presence_of_element_located((By.ID, 'HOGEHOGE') )
檔案 "/selenium/webdriver/support/wait.py", line 71, in until
value = method(self._driver)
檔案 "/selenium/webdriver/support/expected_conditions.py", line 64, in __call__
return _find_element(driver, self.locator)
檔案 "/selenium/webdriver/support/expected_conditions.py", line 415, in _find_element
raise e
檔案 "/selenium/webdriver/support/expected_conditions.py", line 411, in _find_element
return driver.find_element(*by)
檔案 "/selenium/webdriver/remote/webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
檔案 "/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response( response)
檔案 "/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException。訊息。
當我把time.sleep(10)改成下面這樣時,
import time
//登錄程序和它成功了和顯示了以下頁面
login_submit.click()
time.sleep(10)
//授權后的頁面,讓selenium等待,直到``hoge``` 是可點擊的。
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID, 'hoge'/span>))
它像預期的那樣作業。 請注意,最后一部分甚至可以被替換掉
browser.find_element_by_xpath("//select[@id='hoge']"/span>)
我的Python代碼有什么問題?
uj5u.com熱心網友回復:
嘗試使用這個函式來等待元素或東西的出現:
def wait_for(xpath)。
while True。
try:
driver.find_element_by_xpath(xpath)
return True: driver.find_element_by_xpath(xpath)
except NoSuchElementException。
繼續 繼續
不要忘記
from selenium.common.exceptions import NoSuchElementException
你可以使用任何東西,作為方法的XPATH并不重要。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/318348.html
標籤:
