select 标记样式属性设置为显示时如何从下拉列表中检索值:none;在 python 硒中

How to retrieve values from drop down when select tag style attribute is set as display: none; in python selenium

我正在尝试从一个站点中删除所有类别的下拉列表组合。但是,选项的文本属性仅显示为空白。虽然在检查时,我可以看到每个选项都有文本。

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome()
driver.get('https://www.fiyo.nl/')

driver.find_element_by_xpath('//*[@id="select_device_chosen"]/a').click()
select = Select(driver.find_element_by_xpath('//*[@id="select-device"]'))
print ([o.text for o in select.options]) 

输出:

['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']

如果我得到文本,我想遍历所有值以获得其他下拉列表的不同组合。

<select> 标签将 style 属性设置为 display: none; 因此您可以使用以下代码块打印选项:

driver.find_element_by_xpath('//*[@id="select_device_chosen"]/a').click()
element = driver.find_element_by_xpath("//select[@id='select-device']")
driver.execute_script("arguments[0].removeAttribute('style')", element)
select = Select(driver.find_element_by_xpath("//*[@id='select-device']"))
print ([o.text for o in select.options])

如果您像下面这样尝试,样式标签不是考虑的障碍。

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.fiyo.nl/')

driver.find_element_by_xpath('//*[@id="select_device_chosen"]/a').click()
items = ' '.join([item.get_attribute("textContent") for item in driver.find_elements_by_xpath("//*[@class='chosen-results']//*[@class='active-result']")])
print(items.split())

driver.quit()