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_Web Scraping_Display - Fatal编程技术网

当“选择标记样式”属性设置为“显示:无”时,如何从下拉列表中检索值;在python selenium中

当“选择标记样式”属性设置为“显示:无”时,如何从下拉列表中检索值;在python selenium中,python,selenium,selenium-webdriver,web-scraping,display,Python,Selenium,Selenium Webdriver,Web Scraping,Display,我正试图从一个网站上删除所有类别的下拉列表组合。然而,选项的文本属性是空的。虽然在检查时,我可以看到每个选项都有文本 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_

我正试图从一个网站上删除所有类别的下拉列表组合。然而,选项的文本属性是空的。虽然在检查时,我可以看到每个选项都有文本

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]) 
输出:

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

标签的样式属性设置为显示:无因此您可以使用以下代码块打印选项:

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()

谢谢它起作用了。你知道我应该学习什么来自己解决这类问题吗?@BhaveshGhodasara解决这些问题的最好方法是面对它,为自己和其他面临同样问题的社区成员解决它。