java wait 参数,Java中的Java FluentWait

In java selenium-webdriver package, there is a FluentWait class:

Each FluentWait instance defines the maximum amount of time to wait

for a condition, as well as the frequency with which to check the

condition. Furthermore, the user may configure the wait to ignore

specific types of exceptions whilst waiting, such as

NoSuchElementExceptions when searching for an element on the page.

In other words, it is something more than implicit and explicit wait, gives you more control for waiting for an element. It can be very handy and definitely has use cases.

Is there anything similar in python selenium package, or should I implement it myself?

(I've looked through documentation for Waits - nothing there).

解决方案

I believe you can do this with Python, however it isn't packaged as simply as a FluentWait class. Some of this was covered in the documentation you provided by not extensively.

The WebDriverWait class has optional arguments for timeout, poll_frequency, and ignored_exceptions. So you could supply it there. Then combine it with an Expected Condition to wait for elements for appear, be clickable, etc... Here is an example:

from selenium import webdriver

from selenium.webdriver.common.by import By

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

from selenium.common.exceptions import *

driver = webdriver.Firefox()

# Load some webpage

wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException])

element = wait.until(EC.element_to_be_clickable((By.XPATH, "//div")))

Obviously you can combine the wait/element into one statement but I figured this way you can see where this is implemented.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值