0

I am facing problem with Selenium web driver in accessing List box Values.

Check the image: enter image description here

enter image description here

I am able to paste value and pass the class name of list which i want to select, basically i have to select every time the very first option which is displayed in the list

 1   driver.get('https://my.maerskline.com/schedules/vessel')
 2   button = driver.find_element_by_id("s2id_b-vesselCode")   
 3   button.click()        
 4   button1 = driver.find_element_by_id("s2id_autogen1_search")
 5   button1.send_keys(Vessel_name)        
 6   button3 = driver.find_element_by_id("select2-results-dept-0 select2-result select2-result-selectable")
 7   button3.click()
 8   button2 = driver.find_element_by_id("schedulesByVesselSearchButton")
    button2.click()

Line 6 gives me below error

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:
{"method":"id","selector":"select2-results-dept-0 select2-result
select2-result-selectable"}   (Session info: chrome=55.0.2883.87)  
(Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86_64)

Any idea how I can get this done? Any more information needed let me know. Thanks

updated Code

if Vessel_name != "":
        #driver.get('https://my.maerskline.com/vessels?searchTerm={0}'.format(Vessel_name))
        print(Vessel_name)

        driver.get('https://my.maerskline.com/schedules/vessel')
        button = driver.find_element_by_id("s2id_b-vesselCode")   
        button.click()        
        button1 = driver.find_element_by_id("s2id_autogen1_search")
        button1.send_keys(Vessel_name)       
        button3 =driver.find_element_by_css_selector("#select2-results-1>li:nth-child(1)")
        button3.click()
        #button2 = driver.find_element_by_id("schedulesByVesselSearchButton")
        #button2.click()
        try:



        soup = BeautifulSoup(driver.page_source)

3 Answers3

0

You should be locating the element by its class name. The element in the image you provided does not have any id.

Give this xpath a go:

//ul[@id='select2-results-1']/li[@‌​class='selec‌​t2-results-dept-0 select2-result select2-result-selectable select2-highlighted']

If you are sure you always want to select the first li, you can make the xpath alot tidier by referencing its index:

(//ul[@id='select2-results-1']/li)[1]
Cathal
  • 1,318
  • 1
  • 12
  • 19
  • apologies - i am searchig by class name only and the error is – Er Gaurav Chhabra Jan 11 '17 at 13:33
  • Message: invalid selector: Compound class names not permitted (Session info: chrome=55.0.2883.87) (Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86_64) – Er Gaurav Chhabra Jan 11 '17 at 13:33
  • If there is more then one class name assigned to an element, you cannot use the collection of class names in selenium. this is known as a compound class name. So you need to be more specific with your locator. xpath or css would be a better fit here. – Cathal Jan 11 '17 at 13:35
  • cool, if you get stuck..post your attempt here and ill help out. – Cathal Jan 11 '17 at 13:42
  • button3 = driver.find_element_by_xpath("//div[@id='select2-drop']/ul[@id='select2-results-1']/li[@class='select2-results-dept-0 select2-result select2-result-selectable select2-highlighted']") – Er Gaurav Chhabra Jan 11 '17 at 14:12
  • we have div with id select2-drop and then
      and finannly
    – Er Gaurav Chhabra Jan 11 '17 at 14:14
  • Error - unable to locate element :( – Er Gaurav Chhabra Jan 11 '17 at 14:14
  • first normal code is giving me same error, but second code is not giving error ,it is not helping in clicking the vessel name though, it is unable to click on search button...and error is - Message: unknown error: Element – Er Gaurav Chhabra Jan 11 '17 at 15:12
0

as this is drop down with value you should user select class to identify the web-element

you can refer below link to solve this issue :-

Selenium - Python - drop-down menu option value

Community
  • 1
  • 1
PythonUser
  • 706
  • 4
  • 15