繁体   English   中英

将 time.sleep 与 webdriver 一起使用

[英]use time.sleep with webdriver

我对与 webdriver 一起使用的时间模块有问题。 我将时间睡眠设置为 3 秒,但有时我的互联网连接会很慢,并且程序会错过元素并引发错误。

'''
driver.get('exammple.com')
time.sleep(3)
driver.find_element_by_class_name('example')
time.sleep(3)
driver.find_element_by_class_name('example')
'''

我该如何处理这个问题?

你可以做一些投票:

'''
driver.get('exammple.com')
found=False
while(not found):
   elem = driver.find_element_by_class_name('example')
   if elem:
       found=True
found=False
while(not found):
   elem = driver.find_element_by_class_name('example')
   if elem:
       found=True
'''

尝试这个:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
delay = 3 # seconds
myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 
 'IdOfMyElement')))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM