0

I wanted to select the value "LK" in the drop down list on my web page. please help.

i tried to slect by xpath as follows

driver.find_element_by_xpath("//select[contains(text(), 'GLOBAL')]").click()
driver.find_element_by_xpath("//span[contains(text(), 'LK')]/..").click()

but i get following error:

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable

Below is the web element code im trying to fetch:

<select style="border-radius: 3px;" ng-model="selectedRcc.value" ng-options="item for item in ['LK', 'US', 'GLOBAL']" class="ng-pristine ng-valid ng-touched" tabindex="0" aria-invalid="false">
    <option value="0" label="LK">LK</option>
    <option value="1" label="US">US</option>
    <option value="2" selected="selected" label="GLOBAL">GLOBAL</option>
</select>
  • 2
    Possible duplicate of [Selenium - Python - drop-down menu option value](https://stackoverflow.com/questions/7867537/selenium-python-drop-down-menu-option-value) – JeffC May 20 '19 at 18:52
  • Ignore the selected answer and look at [this one](https://stackoverflow.com/a/28613320/2386774) instead. – JeffC May 20 '19 at 18:53

2 Answers2

0

Use the below xpath

//select[@class='ng-pristine ng-valid ng-touched']/option[.='LK']

Code:

driver.find_element_by_xpath("//select[@class='ng-pristine ng-valid ng-touched']/option[.='LK']").click()
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • I am getting following exception. Could you please help `selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//Select[@class='ng-pristine ng-valid ng-touched']/option[.='LK']"} (Session info: chrome=74.0.3729.157) (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17763 x86_64)` – Ruwendra Benjamin May 22 '19 at 09:04
  • Can you check if the list box is part of the current window. `print(len(driver.find_elements_by_xpath("//select[@class='ng-pristine ng-valid ng-touched']"))))`. – supputuri May 22 '19 at 12:36
  • I am getting following output for print:- 0 – Ruwendra Benjamin May 23 '19 at 06:04
  • So, it's not able to identify the list box first place. Can you please check if the list box present in the iframe. – supputuri May 23 '19 at 13:38
0

When you have select element it always good to use selenium select class to select item from drop down menu.

select=Select(driver.find_element_by_xpath("//select[@class='ng-pristine ng-valid ng-touched']"))
select.select_by_visible_text('GLOBAL')
select.select_by_visible_text('LK')

OR you can use index.

select=Select(driver.find_element_by_xpath("//select[@class='ng-pristine ng-valid ng-touched']"))
select.select_by_index(0) #LK
select.select_by_index(2) #GLOBAL

To use above code you need to have following imports.

from selenium.webdriver.support.select import Select
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • I tried both codes but still get an error as follows: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//select[@class='ng-pristine ng-valid ng-touched']"} FYI: I am using python 3 – Ruwendra Benjamin May 21 '19 at 09:30
  • This shouldn't happen.verified on my pc and posted.Try put some sleep before accessing the element and then check. – KunduK May 21 '19 at 09:36
  • Thanks for your help. i'll try that now – Ruwendra Benjamin May 21 '19 at 09:39
  • put some time.sleep(5) before call select element and check if it is working.if not then check any iframe available above your select element in DOM. – KunduK May 21 '19 at 09:41
  • Still i get the same error. code i used: time.sleep(5) select=Select(driver.find_element_by_xpath("//select[@class='ng-pristine ng-valid ng-touched']")) select.select_by_visible_text('GLOBAL') select.select_by_visible_text('LK') – Ruwendra Benjamin May 21 '19 at 09:44
  • I could, but this is a VPN accessible site. I can share the web elements for you as much as you need. `
  • ` – Ruwendra Benjamin May 21 '19 at 09:55
  • I have tried testing on a differrent machine, but i still get the same exception. Could you plz help @KunduK – Ruwendra Benjamin May 22 '19 at 09:12
  • are you on windows or mac or linux? – KunduK May 22 '19 at 09:24
  • window 10 -KunduK – Ruwendra Benjamin May 23 '19 at 06:20