如何解决我在执行 selenium 脚本时遇到问题,执行 Manuel 测试需要更长的时间
例如这个语句需要 41 秒来执行 Manuel 测试:
try:
print("at2-click" + " - Time used=%ss" % int((time.time() - start_time)))
time.sleep(2)
wait_until_visible_then_click(element_by_id)
men_10 = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="product-transaction-area"]/div[1]/div/div/div[2]/div/a[10]')))
men_10.click()
except exp.NoSuchElementException as e:
no_such_element_exption('at2',str(e))
time.sleep(3)
但是这个语句需要 160 秒来执行它的 Manuel 测试
try:
print("In den Wb2 product" + " - Time used=%ss" % int((time.time() - start_time)))
time.sleep(2)
element_by_id = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,'//*[@id="product-transaction-area"]/div[3]/button')))
except exp.NoSuchElementException as e:
no_such_element_exption('In den Wb2',str(e))
try:
print("ZK" + " - Time used=%ss" % int((time.time() - start_time)))
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
element_by_class = WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CLASS_NAME,'button-big')))
except exp.NoSuchElementException as e:
no_such_element_exption('ZK',str(e))
我更喜欢在 50-99 秒之间执行第二条语句,任何人都可以帮忙解决这个问题
解决方法
对我来说最好的解决方案是避免使用 webdriverwait。
你可以做一些池化:
'''
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
'''
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。