硒等待,直到具有给定类的任何元素都可见 [英] Selenium wait until any element with a given class is visible

查看:87
本文介绍了硒等待,直到具有给定类的任何元素都可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些Selenium测试,以确保我的自动完成功能正常工作,并且需要在输入字段中输入文本与尝试从自动完成功能中进行选择之间提供一定的延迟.我不只是使用固定的延迟,而是宁愿等到列表出现.我尝试了以下方法:

I'm writing some Selenium tests to make sure my autocompletes are working properly, and I need to give a bit of a delay between entering text in the input field and trying to select from the autocomplete. Rather than just using a fixed delay, I'd rather wait until the list appears. I've tried the following:

input_element.send_keys("zanzibar c")
WebDriverWait(self.selenium, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, "pac-container")))
input_element.send_keys(Keys.DOWN)

其中pac-container是自动完成列表元素的类.

where pac-container is the class of the autocomplete's list element.

这对于具有自动完成功能的第一个输入有效,但对于其余输入则超时.我猜测是因为它正在等待第一个pac-container可见,而我希望它等待 any 其中一个pac-container可见.

This works for the first input with an autocomplete, but times out for the rest. I'm guessing this is happening because it is waiting for the first pac-container to be visible, whereas I want it to wait for any one of the pac-containers to be visible.

有没有办法等到至少有给定类的一个元素可见?

Is there a way to wait until at least one element with a given class is visible?

推荐答案

我想这就是您要寻找的presence_of_all_elements_located(locator)

I guess this is what you are looking for presence_of_all_elements_located(locator)

class selenium.webdriver.support.expected_conditions.presence_of_all_elements_located(locator)[source]

期望检查是否存在至少一个元素 在网页上. locator用于查找返回列表的元素 定位后的WebElements

An expectation for checking that there is at least one element present on a web page. locator is used to find the element returns the list of WebElements once they are located

参考文献: http://selenium.googlecode.com/git/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html

这篇关于硒等待,直到具有给定类的任何元素都可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆