This question already has an answer here:
I'm trying to select an option from a drop down menu and than to click "Search" but I can't get "select" tag.
The HTML I'm scraping is the following:
<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>
The option I want to select is:
<option label="Caraibi" value="1">Caraibi</option>
The code I'm using is the following:
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')
So, I'm trying to get the "select" tag in different ways and I get different issues.
For example:
1st attempt)
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"))
I get:
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)
2nd attempt)
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"))
I get:
AttributeError: 'list' object has no attribute 'tag_name'
3rd attempt)
driver.find_elements_by_xpath("//select[@class='form-control ng-pristine ng-
untouched ng-valid ng-scope ng-empty']")
I get an empty list:
Out[81]: []
4th attempt)
driver.find_element_by_css_selector(".form-control.ng-pristine.ng-valid.ng-scope.ng-empty.ng-touched")
I get an empty list:
Out[82]: []
5th attempt)
dropdown = driver.find_element_by_xpath("//select[@class='form-control ng-pristine ng-valid ng-scope ng-empty ng-touched']/option[text()= Caraibi]").click()
I get:
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)
Does anybody know how to solve this problem? Thanks in advance!
Best option would be use try except block to catch exceptions while your rest code runs fine. Also your syntax is bit haywire.
Try this: