在java selenium-webdriver软件包中,有一个FluentWait类:


每个FluentWait实例都定义了最长等待时间
条件以及检查频率的频率
健康)状况。此外,用户可以配置等待忽略
等待时的特定类型的异常,例如
在页面上搜索元素时出现NoSuchElementExceptions。


换句话说,它不仅是implicit and explicit wait,还使您可以更好地控制元素的等待。它可能非常方便,并且肯定有用例。

python selenium package中是否有类似内容,还是我应该自己实现?

(我浏览过Waits的文档-那里什么都没有)。

最佳答案

我相信您可以使用Python做到这一点,但是它并不像FluentWait类那样简单地打包。您提供的文档中没有涉及到其中的一些内容。

WebDriverWait类具有用于超时,poll_frequency和ignore_exceptions的可选参数。因此,您可以在那里提供。然后将其与“预期条件”结合使用,以等待元素出现,可点击等。这是一个示例:

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


显然,您可以将wait / element合并为一个语句,但是我发现以这种方式可以看到在哪里实现。

10-01 15:05