Ali*_*man 6 python selenium webdriver selenium-webdriver webdriverwait
我time在我的脚本中使用库:
import time
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
它可以让我的 Selenium WebDriver 休眠一秒钟,但 250 毫秒怎么可能呢?
要将webdriver的执行暂停几毫秒,您可以传递number of seconds或floating point number of seconds如下:
import time
time.sleep(1) #sleep for 1 sec
time.sleep(0.25) #sleep for 250 milliseconds
Run Code Online (Sandbox Code Playgroud)
然而在使用硒和webdriver的用于自动化使用time.sleep(secs)没有任何特定的条件,以实现 失败的目的自动化,应该不惜一切代价避免。根据文档:
time.sleep(secs)在给定的秒数内暂停当前线程的执行。参数可能是一个浮点数,以指示更精确的睡眠时间。实际的暂停时间可能比请求的少,因为任何捕获的信号都会在执行该信号的捕获例程后终止 sleep()。此外,由于系统中其他活动的调度,暂停时间可能比任意数量的请求长。
因此,根据讨论,time.sleep(sec)您应该使用WebDriverWait()in-conjunction withexpected_conditions()来验证元素的状态,并且三个广泛使用的 expected_conditions 如下:
Presence_of_element_located(locator)定义如下:
class selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located
Description : An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible or interactable (i.e. clickable).
Run Code Online (Sandbox Code Playgroud)
visibility_of_element_located(locator)定义如下:
class selenium.webdriver.support.expected_conditions.visibility_of_element_located(locator)
Parameter : locator - used to find the element returns the WebElement once it is located and visible
Description : An expectation for checking that an element is present on the DOM of a page and visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
Run Code Online (Sandbox Code Playgroud)
element_to_be_clickable(locator)定义如下:
class selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)
Parameter : locator - used to find the element returns the WebElement once it is visible, enabled and interactable (i.e. clickable).
Description : An Expectation for checking an element is visible, enabled and interactable such that you can click it.
Run Code Online (Sandbox Code Playgroud)
您可以在WebDriverWait not working as expected 中找到详细的讨论
| 归档时间: |
|
| 查看次数: |
18077 次 |
| 最近记录: |