Reputation: 713
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
Upvotes: 0
Views: 304
Reputation: 713
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.
Upvotes: 0