1

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!

CosimoCD
  • 3,570
  • 3
  • 22
  • 31

1 Answers1

0

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:

try:
    drop = browser.find_elements_by_css_selector('#someID').click() 
except:
    print("Menu not found") 
user3237357
  • 112
  • 9
  • Hi user3237357! I have tried with the css selector but I still get an empty list. – CosimoCD Mar 19 '18 at 12:22
  • First you need to select xpath of drop menu and make it click – user3237357 Mar 19 '18 at 12:33
  • Then use partial_link_text method to search for word you're searching,and then perform operation on it – user3237357 Mar 19 '18 at 12:34
  • I have tried to perform the selection by the xpath but it steel doesn't work (see the 5th attempt) – CosimoCD Mar 19 '18 at 14:11
  • Was the option visible when drop down menu opened.? Because for dynamic pages you need to scroll till that menu so that it gets loaded, and if it's already there, include time.sleep() to provide some details in operations as your option won't be visible till page is loaded. – user3237357 Mar 20 '18 at 02:04