为什么下拉菜单在网页隐藏



我一直在看这个网页,并一直试图使用python中的selenium访问下拉菜单。我有:

from selenium import webdriver
# import selenium.webdriver.support.ui as ui
url = 'http://solutions.3m.com/wps/portal/3M/en_US/Interconnect/Home/Products/ProductCatalog/Catalog/?PC_Z7_RJH9U5230O73D0ISNF9B3C3SI1000000_nid=RFCNF5FK7WitWK7G49LP38glNZJXPCDXLDbl&partNumber=2302-5111-TB'
element_xpath = '//*[@id="Component1"]'
driver = webdriver.PhantomJS()
# wait = ui.WebDriverWait(driver, 20)
driver.get(url)
element = driver.find_element_by_xpath(element_xpath)
print element.is_displayed()
#element_xpath = '/option[@value="02"]'
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
    print option.get_attribute('value'), option.is_displayed()
# wait.until(lambda driver: driver.find_element_by_xpath(element_xpath))
# source = driver.page_source.encode('utf-8', 'ignore')
driver.quit()

我不知道为什么我所要求的没有一个是可见的(即x.is_displayed() == True),当我可以清楚地看到它在网页上。我也在Firefox上试过。

奇怪的是,有时(比如,5%的时间),这些东西是可见的。但是百分之五并没有真正的帮助。

提示吗?

下拉字段的选项只有在单击下拉选项时才可见。所以在检查all_options之前你应该执行click():

element = driver.find_element_by_xpath(element_xpath)
element.click()
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
    print option.get_attribute('value'), option.is_displayed()

它在我的测试中工作,但我使用driver = webdriver.Firefox()

最新更新