WebDriverWait(driver, 20) 是什么意思?
What does WebDriverWait(driver, 20) means?
我正在使用以下 selenium 代码:
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
PATH= r"C:\Users\Hamid\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://www.google.com/")
click_button=driver.find_element_by_xpath('//*[@id="L2AGLb"]/div').click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")
search=driver.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]').click()
不过我不确定以下代码行的细分是什么:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")
webdriverwait 是什么意思? 20指的是什么? EC元素是什么意思?为什么需要等待? q指的是什么?如果我想使用相同的代码来处理页面上的不同项目,我通常会更改什么?
根据 WebDriverWait 的 API 文档,构造函数将 WebDriver 实例和以秒为单位的超时作为参数。
class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
where,
driver: Instance of WebDriver (Ie, Firefox, Chrome or Remote)
timeout: Number of seconds before timing out
poll_frequency: sleep interval between calls By default, it is 0.5 second.
ignored_exceptions: iterable structure of exception classes ignored during calls. By default, it contains NoSuchElementException only.
例如,使用强制参数:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
使用 lambda 表达式:
element = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "someId"))
使用所有参数:
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
我正在使用以下 selenium 代码:
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
PATH= r"C:\Users\Hamid\Desktop\Selenium\chromedriver.exe"
driver=webdriver.Chrome(PATH)
driver.get("https://www.google.com/")
click_button=driver.find_element_by_xpath('//*[@id="L2AGLb"]/div').click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")
search=driver.find_element_by_xpath('/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]').click()
不过我不确定以下代码行的细分是什么:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")
webdriverwait 是什么意思? 20指的是什么? EC元素是什么意思?为什么需要等待? q指的是什么?如果我想使用相同的代码来处理页面上的不同项目,我通常会更改什么?
根据 WebDriverWait 的 API 文档,构造函数将 WebDriver 实例和以秒为单位的超时作为参数。
class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)
where,
driver: Instance of WebDriver (Ie, Firefox, Chrome or Remote)
timeout: Number of seconds before timing out
poll_frequency: sleep interval between calls By default, it is 0.5 second.
ignored_exceptions: iterable structure of exception classes ignored during calls. By default, it contains NoSuchElementException only.
例如,使用强制参数:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))
使用 lambda 表达式:
element = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "someId"))
使用所有参数:
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())