如何使用Selenium和python从下拉列表中获取值列表 [英] How to get list of values from dropdown list with selenium and python

查看:314
本文介绍了如何使用Selenium和python从下拉列表中获取值列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从下拉列表中获取所有值的列表.

I've tried to get list of all values from dropdown.

问题是单击下拉列表后激活了该下拉列表.值列表仅在单击下拉菜单后显示

The problem is the dropdown is activated after click on it. The list of values shows only after click on dropdown

现在我有了这样的代码:

Now I've got code like this:

browser.find_element_by_xpath("/html/body/div[3]/div[8]/div[2]/div[1]/div[4]").click()

它使我能够激活下拉菜单,但我不完全了解如何从该下拉菜单中提取所有可能的值

It enable me to activate dropdown but I don't know completely how to extract all possible values from this dropdown

<option value="">-</option><option value="0401">Aleksandrowski</option><option value="2001">Augustowski</option> 

推荐答案

使用根据最佳做法,您可以添加 WebDriverWait expected_conditions 如下:

As per the best practices, you can add WebDriverWait and expected_conditions as follows:

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

locator = "/html/body/div[3]/div[8]/div[2]/div[1]/div[3]/div/div/select[1]"
botton_to_select = WebDriverWait(driver, 10).until((EC.element_to_be_clickable, (By.XPATH, locator)))
botton_to_select.click()
select = Select(browserdriver.find_element_by_xpath(locator))
for item in select.options:
    print(item.get_attribute('innerText'), item.get_attribute('value'))

希望这对您有所帮助!

这篇关于如何使用Selenium和python从下拉列表中获取值列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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