[英]use time.sleep with webdriver
I have a problem with time module in use with webdriver.我对与 webdriver 一起使用的时间模块有问题。 I set time sleep for 3 second but sometime my internet connection will be slow and program miss the element and raise error.
我将时间睡眠设置为 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')
'''
how can I handle this problem??我该如何处理这个问题?
You can do some polling:你可以做一些投票:
'''
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
'''
Try this:尝试这个:
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.