我试图从下拉菜单中选择一个选项,然后点击“搜索”,但我无法得到“选择”标签。在
我正在抓取的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>
我想选择的选项是:
^{pr2}$我使用的代码如下:
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]: []
第五次尝试)
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)
有人知道怎么解决这个问题吗? 提前谢谢!在
最好的选择是使用try-except块在rest代码运行良好时捕捉异常。 你的语法也有点混乱。在
试试这个:
相关问题 更多 >
编程相关推荐