Python selenium无法选择下拉列表

时间:2018-03-19 12:00:54

标签: python selenium selenium-chromedriver

我尝试从下拉菜单中选择一个选项,而不是点击"搜索"但我无法选择"选择"标签。

HTML I'抓取如下:

 <select class="form-control ng-pristine ng-untouched ng-valid ng-scope ng-
    empty" ng-class="{ 'select_selected' : selected.destinationList}" ng-
    model="selected.destinationList" ng-if="!bIsLoading" ng-
    change="applyPrefetch()" ng-disabled="bSearchLoading" ng-
    options="maps.itineraries[dest].Name for dest in prefetch.itineraries 
    track by dest">
 <option value="" selected="selected" class="">Seleziona
 destinazione</option>
 <option label="Caraibi" value="1">Caraibi</option>
 <option label="Emirati Arabi" value="2">Emirati Arabi</option>
 <option label="Giro del Mondo" value="3">Giro del Mondo</option>
 <option label="America" value="4">America </option>

 </select>

我想选择的选项是:

<option label="Caraibi" value="1">Caraibi</option>

我使用的代码如下:

from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome(executable_path=r"C:example\chromedriver.exe")

# Open the url
driver.get('https://www.examplesite.com')

# select by css selector
select = Select(driver.find_elements_by_css_selector(".form-control"))

# select by visible text
select.select_by_visible_text('Caraibi')

所以,我试图让#34;选择&#34;标签以不同的方式,我得到不同的问题。

例如:

第一次尝试

select = Select(driver.find_elements_by_class_name("form-control ng-valid ng-scope ng-not-empty ng-dirty ng-valid-parse select_selected ng-touched"))

我明白了:

InvalidSelectorException: invalid selector: Compound class names not 
permitted
(Session info: chrome=64.0.3282.186)
(Driver info: chromedriver=2.32.498550 
(9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 6.1.7601 SP1 
x86_64)

第二次尝试

select = Select(driver.find_elements_by_class_name(".form-control.ng-
valid.ng-scope.ng-not-empty.ng-dirty.ng-valid-parse.select_selected.ng-
touched"))

我明白了:

AttributeError: 'list' object has no attribute 'tag_name'

第3次尝试

driver.find_elements_by_xpath("//select[@class='form-control ng-pristine ng-
untouched ng-valid ng-scope ng-empty']")

我得到一个空列表:

Out[81]: []

第4次尝试

driver.find_element_by_css_selector(".form-control.ng-pristine.ng-valid.ng-scope.ng-empty.ng-touched")

我得到一个空列表:

Out[82]: []

第5次尝试

dropdown = driver.find_element_by_xpath("//select[@class='form-control ng-pristine ng-valid ng-scope ng-empty ng-touched']/option[text()= Caraibi]").click()

我明白了:

NoSuchElementException: no such element: Unable to locate element: 
{"method":"xpath","selector":"//select[@class='form-control ng-pristine ng-
valid ng-scope ng-empty ng-touched']/option[text()= Mediterraneo]"}
(Session info: chrome=64.0.3282.186)
(Driver info: chromedriver=2.32.498550 
(9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 6.1.7601 SP1 
 x86_64)

有人知道如何解决这个问题吗? 提前致谢!

1 个答案:

答案 0 :(得分:0)

最好的选择是使用try除了块来捕获异常,而你的其余代码运行正常。 你的语法也有点乱。

试试这个:

try:
    drop = browser.find_elements_by_css_selector('#someID').click() 
except:
    print("Menu not found")