srikanth sharma srikanth sharma - 3 months ago 26
Python Question

Select dropdown in AngularJS application from Python Selenium

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
??

Answer

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");