Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python和Selenium Webdriver存储动态下拉选项_Python_Selenium_Xpath_Selenium Webdriver_Webdriver - Fatal编程技术网

使用Python和Selenium Webdriver存储动态下拉选项

使用Python和Selenium Webdriver存储动态下拉选项,python,selenium,xpath,selenium-webdriver,webdriver,Python,Selenium,Xpath,Selenium Webdriver,Webdriver,我试图存储由邮政编码查找生成的地址值,然后创建一个列表,我可以使用python随机模块使用Random.choice选择一个随机值 情景: 输入邮政编码,单击“搜索”-下拉列表将动态填充可用选项 我使用字典将表单值存储为xpath,然后使用webdriver通过xpath查找元素\u或通过xpath查找元素 代码如下所示(格式不正确,仅供参考): 在我的邮政编码上使用随机模块不是问题 如果有人能给我一些指导或给我指明一个参考方向,我将不胜感激——我只是Selenium和Python的一个新手——

我试图存储由邮政编码查找生成的地址值,然后创建一个列表,我可以使用python随机模块使用Random.choice选择一个随机值

情景:

输入邮政编码,单击“搜索”-下拉列表将动态填充可用选项

我使用字典将表单值存储为xpath,然后使用webdriver通过xpath
查找元素\u
通过xpath查找元素

代码如下所示(格式不正确,仅供参考):

在我的邮政编码上使用随机模块不是问题

如果有人能给我一些指导或给我指明一个参考方向,我将不胜感激——我只是Selenium和Python的一个新手——在这个问题上取得了稳步的进展,但似乎在绕圈子——我的第一个问题是使用
find_element_by_xpath
一个简单的“s”missing off“element”将我抛在一边而。

使用python selenium绑定提供的开箱即用类-它是
select->option
HTML结构上的一个很好的抽象层:

from selenium.webdriver.support.ui import Select

# initialize the select instance
select = Select(driver.find_element_by_id('address'))

# get the list of options and choose a random one
options = [o.text for o in select.options]
option = random.choice(options)

# select it
select.select_by_visible_text(option)

谢谢你。正是我要找的东西。非常感谢。
from selenium.webdriver.support.ui import Select

# initialize the select instance
select = Select(driver.find_element_by_id('address'))

# get the list of options and choose a random one
options = [o.text for o in select.options]
option = random.choice(options)

# select it
select.select_by_visible_text(option)