Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.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

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为选项选择下拉列表_Python_Selenium_Selenium Webdriver - Fatal编程技术网

Python Selenium为选项选择下拉列表

Python Selenium为选项选择下拉列表,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,在过去的几天里,我一直在尝试选择一个下拉列表,至少打印出可用的选项,但我就是无法让它工作 当我运行模块时,我得到了这个错误 回溯(最近一次呼叫最后一次): 文件“sel_test_elements2.py”,第20行,在 打印([select_element.options中o的o.text]) AttributeError:'FirefoxWebElement'对象没有属性'options' 目前我的代码是这样的 from selenium.webdriver.support.ui impor

在过去的几天里,我一直在尝试选择一个下拉列表,至少打印出可用的选项,但我就是无法让它工作

当我运行模块时,我得到了这个错误

回溯(最近一次呼叫最后一次): 文件“sel_test_elements2.py”,第20行,在 打印([select_element.options中o的o.text]) AttributeError:'FirefoxWebElement'对象没有属性'options'

目前我的代码是这样的

from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By

# Define Global Variables
url = "https://games.pcaha.ca/teams/4329"
csv_file = "game_schedule_4329.csv"
games = []

# create a new Firefox session
driver = webdriver.Firefox()
driver.get(url)
driver.implicitly_wait(30)

# Locate the Sector and create a Select object
select_element = driver.find_element_by_css_selector(".team-filters")
# this will print out strings available for selection on select_element, used in visible text below
print ([o.text for o in select_element.options])```



我在我写的一个小脚本中使用了类似的东西,也许它可以给你一个关于如何进行的提示

方法1这是选择最后一个可用选项

下面代码中的变量
options
将成为下拉列表中可用的选项

select_datebox = driver.find_element_by_id('jrnyDateSrchTxt') # Drop down selection, you have to change the id appropriately
select_datebox.click()
time.sleep(2)
options = select_datebox.find_elements_by_tag_name('option')
options[len(options)-1].click() #selecting the last option
方法1通过变量输入选项

select = Select(driver.find_element_by_id("jrnyDateSrchTxt")) # Drop down selection, you have to change the id appropriately
time.sleep(1)
select.select_by_value(datadate) # Date selection
time.sleep(2)

您面临的问题是,此网站正在使用react,而不使用默认的选择和选项。它们实现了一个自定义下拉列表,因此与之交互的方式与与与常规web元素交互的方式相同,在这种情况下,选择和选项将不起作用

我修改了你的代码,它在Chrome中对我有效:

from selenium.webdriver import Chrome
from time import sleep

# Define Global Variables
url = "https://games.pcaha.ca/teams/4329"
csv_file = "game_schedule_4329.csv"
games = []

# create a new Chrome session
driver = Chrome()
driver.get(url)
driver.implicitly_wait(30)

sleep(3) # make sure svgs load before interaction

# Click on arrow down
arrow = driver.find_elements_by_css_selector(".team-filters svg")[1].click()

# Collect options
options = driver.find_elements_by_xpath("//div[contains(@id, 'react-select-2')]")

# Print text from options
print([o.text for o in options])
注意:当在浏览器中手动打开下拉列表并尝试使用web inspector时,它会关闭,因此为了在下拉列表中获取html,您可以使用以下方法:

dropdown = driver.find_element_by_css_selector("div.css-kj6f9i-menu")
dropdown_html = dropdown.get_attribute('innerHTML')

我希望这有帮助。祝你好运

这是成功的,非常感谢你的帮助,我现在将继续我的脚本,因为我需要选择一个下拉选项,然后拉游戏的时间表。