Python Selenium:选择选项

时间:2016-05-05 12:11:10

标签: python selenium-webdriver

我必须从下拉菜单中选择一个选项,HTML代码是

<select id="formIdVeic" style="width: 250px; display: none;" class="combobox-f combo-f" comboname="formIdVeic">
    <option value="" selected="selected"></option>
    <option value="E">TARGA ESTERA </option>
    <option value="J">TARGA FILOBUS </option>
    <option value="L">TELAIO </option>
    <option value="TN" selected="selected">TARGA ITALIANA NUOVO FORMATO </option>
    <option value="TS">TARGA ITALIANA SPECIALE </option>
    <option value="TV">TARGA ITALIANA VECCHIO FORMATO </option>
    <option value="X">TARGA PROVA </option>
    <option value="Y">TARGA PROVVISORIA </option>
</select>

我想选择&#34; TV&#34;选项。我已经解决的问题是

from selenium.webdriver.support.select import Select
...
a=Select(browser.find_element_by_xpath('//select[@id="formIdVeic"]'))
a.select_by_visible_text("TARGA ITALIANA VECCHIO FORMATO").click()

a.select_by_value("TV").click()

其中browser是Web浏览器驱动程序。这两个解决方案都返回了一个例外:

ElementNotVisibleException: element not visible: 
Element is not currently visible and may not be manipulated

我在哪里做错了?

1 个答案:

答案 0 :(得分:1)

您无法在Selenium中选择不可见元素。 可见性检查的来源 -

https://github.com/SeleniumHQ/selenium/blob/master/javascript/atoms/dom.js#L577