1

I need to select an element/item from a drop-down menu that doesn't have an id element using Python and Selenium.

The piece of HTML code:

<mbo-transaction-mode-select mode="filters.mode" class="ng-isolate-scope">
    <select class="form-control input-sm ng-pristine ng-untouched ng-valid ng-empty" ng-model="mode" ng-options="value for (key,value) in vm.modes">
        <option value="" class="" selected="selected"></option>
        <option label="LIVE" value="string:LIVE">LIVE</option>
        <option label="TEST" value="string:TEST">TEST</option>
    </select>

The current option I found on Stackoverflow or Google used the Select method, but that option used find_element_by_id which I unfortunately don't have. I tried to use:

select = Select(browser.find_element_by_xpath("//input[@ng-model='mode']"))
select.select_by_visible_text('LIVE')

But this gave the error:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //input[@ng-model='mode']

Is there another way for me the select the dropdown and one of its options?

JeBo
  • 187
  • 1
  • 3
  • 12
  • Check out https://stackoverflow.com/questions/7867537/how-to-select-a-drop-down-menu-value-with-selenium-using-python – Pinaki Jan 23 '20 at 08:19
  • Can you locate `input[ng-model='mode']` in Dev Tools? – Sers Jan 23 '20 at 08:43
  • @Pinaki, that is what I was referring to with the issue that its using finding an element by ID, which isn't an option for me :( – JeBo Jan 23 '20 at 08:45
  • @Sers, I'm unaware how to use Dev Tools. – JeBo Jan 23 '20 at 08:45
  • [How to search element in Devtools](https://www.toolsqa.com/selenium-webdriver/inspect-elements-with-chrome-developer-tools/). Can you share the URL? – Sers Jan 23 '20 at 08:48
  • @Sers Unfortunately it's behind a login. I did run the Dev tools search using input[ng-model='mode'], but _No matches_ found.... Altough the HTML code provides: ng-model="mode" – JeBo Jan 23 '20 at 08:54

1 Answers1

3

You need to fix your xpath, as here:

element = browser.find_element_by_xpath("//select[@ng-model='mode']")
driver.execute_script("arguments[0].scrollIntoView();", element)
select = Select(element)
select.select_by_visible_text('LIVE')
Evgeniy Chiruk
  • 358
  • 1
  • 10
  • You can find more information about xpath here: https://www.guru99.com/xpath-selenium.html – Evgeniy Chiruk Jan 23 '20 at 08:57
  • @Evhgeniy Chiruk Now I have the problem with using select, that I won't be able to run this headless/unatended.... Is this avoidable? – JeBo Jan 24 '20 at 09:56
  • What exception are you getting? – Evgeniy Chiruk Jan 24 '20 at 10:00
  • @Evhgeniy Chiruk, When running headless it gives: _selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated_ (or trough a Scheduled Task it automatically runs the script, non headless, in the background and I guess same error occurs) – JeBo Jan 24 '20 at 10:03
  • Add this: `((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);` before selecting by text – Evgeniy Chiruk Jan 24 '20 at 10:12
  • I still get a _SyntaxError: invalid syntax_ for the _(JavascriptExecutor) browser)_ part (specifically the browser) – JeBo Jan 24 '20 at 10:54
  • I'm sorry, I just recently answered such a question with java and inserted the code from Java) I updated the answer) – Evgeniy Chiruk Jan 24 '20 at 10:57
  • No problem, really appreciatie the help..!! Now i get: _TypeError: Object of type Select is not JSON serializable_ for that line – JeBo Jan 24 '20 at 10:59
  • driver.execute_script = browser.execute in my case right? Then i get the _selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated_ again :( – JeBo Jan 24 '20 at 11:04
  • Right. But does the same code work without --headless? – Evgeniy Chiruk Jan 24 '20 at 11:06
  • Yeah, your original version already worked on headless. The problem I need is to get it working headless, cause it needs to run during midnight (pref. through a task scheduler).... I guess I phrased my first comment wrong? :( – JeBo Jan 24 '20 at 11:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206576/discussion-between-evgeniy-chiruk-and-jebo). – Evgeniy Chiruk Jan 24 '20 at 11:15