2

I wanted to test page in Python Selenium, which uses AngularJS. It contains a select option and I have tried a number of options to select it but it fails.

<select id="xtzpp001" ng-model="..." ng-options="...." ng-change="changeView()">
  <option value class>select</option>
  <option value="200" selected="selected">Student 1</option>
  <option value="201">Student 2</option>
  <option value="202">Student 3</option>
</select>

As I said, I tried---

driver.find_element_by_xpath('//select[@id="xtzpp001"]/option[contains(text(), "Student 2")]').click()

then

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('xtzpp001'))
for i in select.options:
   .......etc etc

I used explicit waits also.

I came to know that pytractor can help me in this situation (not being actively developed though), but I am unable to find any documentation regarding select option using pytractor.

Is there any other way to achieve this?? or pytractor equivalent?

Or can I use protractor(or any javascript framework) to continue in testing in Javascript from this stage and integrate/callback the results in Python??

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Srikanth Sharma
  • 1,509
  • 2
  • 15
  • 27

3 Answers3

0

Use the Select class from selenium.webdriver.support.ui. Pass the select webelement to the constructor. Use selectByVisibleText. Refer to the answer by @alecxe in the post - Selenium - Python - drop-down menu option value

Community
  • 1
  • 1
Grasshopper
  • 8,908
  • 2
  • 17
  • 32
0

If you have tried all the possible attempt but never got success, you should try using execute_script() as below :-

element = driver.find_element_by_id("xtzpp001")

driver.execute_script("var select = arguments[0]; for(var i = 0; i < select.options.length; i++){ if(select.options[i].text == arguments[1]){ select.options[i].selected = true; } }", element, "Student 2");
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
-1

you can also use Actions to click the menu item, here is an example:

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
kshitij Nigam
  • 44
  • 1
  • 8