微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python Selenium无法选择下拉菜单

这个问题已经在这里有了答案:            >            Compound class names not permitted error Webdriver                                    6个
>            Invalid selector: Compound class names not permitted using find_element_by_class_name with Webdriver and Python                                    2个
我正在尝试从下拉菜单中选择一个选项,然后单击“搜索”,但是我无法获得“选择”标签.

我要抓取的HTML如下:

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

因此,我试图以不同的方式获取“ select”标签,并且遇到不同的问题.

例如:

第一次尝试)

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'

第三次尝试)

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

我得到一个空列表:

Out[81]: []

第四次尝试)

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)

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

解决方法:

最好的选择是使用tryexcept块来捕获异常,而您的其余代码运行良好.
另外,您的语法有点麻烦.

尝试这个:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐