0

I'm trying to get the drop-down options of a selector. When I'm accessing the website with Chrome, and use F12 to look at the HTML, the part I'm dealing with looks like this:

<select name="routeSelector" onkeypress="selectorKeyPress(this, event);
    return false;" onfocus="clearKeyBuffer(this)" 
    onchange="selected(this);routeSelected()">
<option value="701">701</option>
<option value="702">702</option>
<option value="703">703</option>
<option value="104">104</option>
... etc
</select>

However, when I use selenium (java) to access this page, I get

<select name="routeSelector" onkeypress="selectorKeyPress(this, event);return false;" onfocus="clearKeyBuffer(this)" onchange="selected(this);routeSelected()">
                          <option selected="selected">
                            _________________________
                          </option>
                        </select>

Thus I cannot get the options.

My code looks like this:

public class Foo {
public static void main(String[] args) {

    HtmlUnitDriver driver = new HtmlUnitDriver();   
    try {
        driver.get("http://***.html");
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    driver.setJavascriptEnabled(true);
    Select select = new Select(driver.findElement(By.name("routeSelector")));
    List<WebElement> options = select.getOptions(); // return only 1 blank element as above
    System.out.print(driver.getPageSource()); 
    driver.quit();
}

I've also tried to perform click on the selector, or refreshing the page, which all doesn't work. I'm guessing there is some Javascript on this webpage involved?

Thanks in advance!

-- Java version 7 Selenium version 2

2
  • I's it possible to give a link to the webpage you're testing.And can you try testing it with chromeDriver or firefoxDriver instead of HTMLUnit Commented Jul 5, 2015 at 8:24
  • 1
    @Madhan thanks for your suggestion! Actually I've solved this problem after working around. I'll put an answer later. Commented Jul 25, 2015 at 1:58

1 Answer 1

0

After reading the source codes of that page I've solved this problem.

So I found a piece of JavaScript code from the page:

parent.addOption(routeSelector, "701", "701", false, false);

parent.addOption(routeSelector, "702", "702", false, false);

parent.addOption(routeSelector, "703", "703", false, false);

parent.addOption(routeSelector, "104", "104", false, true);

And somehow this piece of code gets executed when the page is loaded. If you want to do it in selenium you have to execute these codes manually. After doing it manually the select.getOptions() method could return the options.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.