报错代码
WebDriverWait(driver, 10, 0.5, ignored_exceptions=None).until(EC.presence_of_element_located(By.ID, 'su1'), '找不到')
改正:presence_of_element_located(())有两层括号
WebDriverWait(driver, 10, 0.5, ignored_exceptions=None).until(EC.presence_of_element_located((By.ID, 'su1')), '找不到')
显示等待理论介绍
#显示等待
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)
1、通过from selenium.webdriver.support.wait import WebDriverWait导入模块
driver:浏览器驱动
timeout:最长超时时间,默认以秒为单位
poll_frequency:检测的间隔步长,默认为0.5s
ignored_exceptions:超时后的抛出的异常信息,默认抛出NoSuchElementExeception异常
2、until和until_not
WebDriverWait(driver,10).until(method,message)
# until:当某元素出现或什么条件成立则继续执行
WebDriverWait(driver,10).until_not(method,message)
# nutil_not:当某元素消失或什么条件不成立则继续执行
# method: 在等待期间,每隔一段时间调用这个传入的方法,直到返回值不是False
# message: 如果超时,抛出TimeoutException,将message传入异常
在设置时间(10s)内,等待后面的条件发生。如果超过设置时间未发生,则抛出异常。在等待期间,每隔一定时间(默认0.5秒),调用until或until_not里的方法,直到它返回True或False.
3、
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
# 隐式等待
# driver.implicitly_wait(10)
# 显示等待
# 此处注意,如果省略message=“”,则By.ID外面是三层()
WebDriverWait(driver, 10, 0.5, ignored_exceptions=None).until(EC.presence_of_element_located((By.ID, 'su1')), '找不到')
driver.quit()
https://blog.csdn.net/sinat_41774836/article/details/88965281
最初执行的代码一开始是这样写:presence_of_element_located(By.ID,“su”),这样相当于取到了3个参数(self, By.ID, “su”)
而presence_of_element_located类中__init__()方法取的确实是2个参数(self, locator),其中locator调用的是一个tuple(元组)
该元组(By.ID,“su”)作为一个整体,对应相当于1个参数,加上类实例化代表自身的self参数,正好就是2个参数
因此,执行代码正确的写法为:presence_of_element_located((By.ID,“su”)),即需要嵌套两层英文圆括号