为什么隐藏网页中的下拉菜单

时间:2014-06-11 17:52:05

标签: python selenium selenium-webdriver html-select

我一直在查看this网页,并尝试使用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%的时间),事物是可见的。但是5%并不是真的有用。

任何提示?

1 个答案:

答案 0 :(得分:0)

如果单击下拉选项,则只有可以看到下拉字段的选项。 因此,在检查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()

相关问题