• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

selenium设置WebDriverWait显示等待时间报错解决方法

武飞扬头像
Dream_flying@
帮助1

#三种等待方式:1、sleep(实际项目不建议使用影响项目性能,用来调式脚本)

2、implicitly_wait(隐式等待,一次设定多次使用)

3、WebDriverWait(显示等待,随时使用) 参数:driver timeout poll_frequency ignored_exceptions

#两种方法 until 与 until_not 参数: method ,message

from selenium import webdriver
from time import sleep
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

class TestCase:
def init(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.get(‘https://www.imooc.com/’)
print(self.driver.title)

def test_sleep(self):
    self.driver.find_element_by_id('kw').send_keys('selenium')
    sleep(2)   #线程阻塞 blocking wait
    self.driver.find_element_by_id('su').click()
    sleep(2)
    self.driver.quit()
def test_implicitly_wait(self):
    self.driver.implicitly_wait(10)
    self.driver.find_element_by_id('kw').send_keys('selenium')
    self.driver.find_element_by_id('su').click()
    self.driver.quit()
def test_wait(self):
    wait = WebDriverWait(self.driver,2)#实例化 WebDriverWait
    wait.until(EC.title_is("百度一下你就知道"))
    self.driver.find_element_by_id('kw').send_keys('selenium')
    self.driver.find_element_by_id('su').click()
    self.driver.quit()
学新通

if name == ‘main’:
case = TestCase()
#case.test_sleep()
#case.test_implicitly_wait()
case.test_wait()
调用case.test_wait方法是报错
学新通
最后发现是标题写的有误,然后修改这句代码中的标题后运行正常:wait.until(EC.title_is(“百度一下,你就知道”))。
平时总结写代码是要特别注意细节问题

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfkacgb
系列文章
更多 icon
同类精品
更多 icon
继续加载