Selenium Select 到 return 列表?

Selenium Select to return a list?

在 python 中我们有:

select = Select(driver.find_element_by_id('fruits01'))

但是如果我想要一个包含所有 select 个标签的列表,而不仅仅是第一个呢?

我试过了:

select = Select(driver.find_elements_by_id('fruits01'))

但它对我不起作用。

elems=driver.find_elements_by_id('fruits01')
for elem in elems:
   select=Select(elem)

为什么不直接使用列表来循环它们呢?我不认为你可以按照你想要的方式去做。

根据 Select() 的 Selenium Python API 文档:

class selenium.webdriver.support.select.Select(webelement)
    A check is made that the given element is, indeed, a SELECT tag. If it is not, then an UnexpectedTagNameException is thrown.

所以Select()接受一个webelement作为参数,你赢了无法通过webelements.

此外,每个网络元素在 DOM Tree 中都是唯一的。因此,使用相同的 id 属性值(即 fruits01.

来识别多个元素的可能性很小

这个用例

但是,将用例视为有效用例,最简单的方法类似于 @Arundeep Chohan but ideally inducing for the visibility_of_all_elements_located(),如下所示:

elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.ID, "fruits01")))
for element in elements:
    select = Select(element)

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC