什么是WebDriverWait(Driver,20)'意思是?

发布于 2025-01-22 17:04:14 字数 916 浏览 0 评论 0原文

我正在处理以下硒代码:

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指的是什么?

如果我想使用相同的代码在页面上的其他项目上使用,我通常会更改什么?

I am working with the following Selenium code:

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()

I am not sure however what the breakdown for the following line of code is:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("ONS data")

What does WebDriverWait mean? What does 20 refer to? What is meant by the EC element? Why does one need to wait? What does q refer to?

If I wanted to use this same code to work on a different item on the page, what would I typically change?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入画浅相思 2025-01-29 17:04:14

根据 webdriverwait 构造函数将Web驱动程序的实例和超时以几秒钟作为参数。

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())

As per the API documentation of WebDriverWait the constructor takes a WebDriver instance and timeout in seconds as arguments.

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.

As an example, using the mandatory arguments:

element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q")))

Using lambda expression:

element = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "someId"))

Using all the arguments:

is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文