Deepak Goel
Deepak Goel

Reputation: 5684

How to select any option of select tag using android web driver

I am writing test cases for a website for android device. In which I need to select an option from the drop down list of the page. But it seems that android web driver does not provide any solution regarding it.

I tried the Select API but it is not working.

Code snippet

      Select loginType = new Select(this.driver.findElement(By.xpath(LOGIN_TYPE_CHOICE)));
      loginType.selectByValue("smartphone");
      driver.findElement(By.id(LOGIN_BUTTON)).click();

Looking for some workaround.

Upvotes: 2

Views: 1181

Answers (3)

Franz Fischbach
Franz Fischbach

Reputation: 46

I'm using c# to run selenium tests against android, firefox Chrome and IE and I enounterd the same problem with the android driver.

This worked for me : (it should work in java if you refactor the code according to the Java conventions)

string jsToBeExecuted="document.getElementById('<insert dropdown Id here>').selectedIndex=<insert index here>";

IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)this.Driver;
jsExecutor.ExecuteScript(jsToBeExecuted);

Note that no changes will be rendered on screen ! ! !

Upon submitting the element with the selected index will be used. It is up to you if you want to add some tweaks to it to select the elements by text or whatever you like.

Upvotes: 3

AlekseiPetrovski
AlekseiPetrovski

Reputation: 1106

here is solution for Ruby:

To select value from list needs to execute javascript, here is example:

HTML:

<select id="select_id">
   <option id="option_id_1" value="value_1" name="OPTION1"> OPTION1 </option>
   <option id="option_id_2" value="value_2" name="OPTION2"> OPTION2 </option>

Updated: much easier way: $('#select_id').val(value_1)

Code: find elements by id attribute:

browser.execute_script("$('#select_id').val($('#option_id_1').val())")

find elements by name attribute:

browser.execute_script("$('#select_id').val($('option[name=OPTION2]').val())")

Works perfectly for me.

Upvotes: 0

eugene.polschikov
eugene.polschikov

Reputation: 7339

I have the following assumption from my expirience of automation web applications with selenium. as I know selenium is uncapable of interacting direcrly with dropdown options as they considered to be invisible (inactive). The way it always works - is to use js for this. First of all locate element properly with css selector properly and verify it with firepath (addon to firebug in ffox) enter image description here

So you got css sselector:

String css=....;
public void jsClickByCss(String cssLocator){
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssLocator+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());
}
jsClickByCss(css);

You can also try another approach using Builder, advanced user interactions API: The idea is quite simple. First of all you should make dropdown roll down so element you want to click on become visible , wait with driver.manage.timeout and then select needed dropdown element and click.

WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();

Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.MoveToElement(mnEle).perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click();

You can read some additional info here Hope this works for you)

Upvotes: 0

Related Questions