我正在尝试废弃一个网站的所有下拉类别组合。但是,选项的文本属性仅为空白。尽管在检查时,我可以看到每个选项都存在文本。
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])
关于python - 当选择标签样式属性设置为显示: none; in python selenium时如何从下拉列表中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49705523/