【selenium踩坑系列】隱式等待 報錯value must be a non-negative integer
阿新 • • 發佈:2022-03-06
本文記錄selenium踩過的坑,每踩一個坑,都不能忘記避坑的方法。
執行以下程式碼時,使用selenium的隱式等待出現報錯:
class TestCase_login():
@classmethod
def setup_class(cls):
cls.driver = browser('chrome')
# 隱式等待10秒
cls.driver.implicitly_wait(10)
報錯資訊如下
selenium.common.exceptions.WebDriverException: Message: invalid argument: value must be a non-negative integer
疑惑了一會為什麼會報“值必須是一個非負整數”這種錯,百度後找到解決方法,是selenium版本的問題,我的版本是selenium 3.5,解除安裝selenium 3.5,重新下載selenium-3.141.0之後,執行程式,問題解決。
安裝命令:
pip install selenium==3.141.0
以下程式碼可測試隱式等待是否生效
from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from time import ctime driver = webdriver.Chrome() # implicitly_wait是隱式等待 # 判斷某元素,如果超過10秒未發現,則丟擲異常 # 如果在10秒內發現,則對該元素進行操作 driver.implicitly_wait(10) driver.get("https://www.baidu.com") try: print(ctime()) driver.find_element_by_xpath("//*[@id='kww']").send_keys('Bela') driver.find_element_by_xpath("//*[@id='su']").click() except NoSuchElementException as e: print(e) finally: print(ctime())
本文來自部落格園,作者:是小魚呀,轉載請註明原文連結:https://www.cnblogs.com/sophia12138/p/15969727.html