使用Selenium webdriver Java时无法获取下拉选项?

使用Selenium webdriver Java时无法获取下拉选项?,java,html,selenium,selenium-webdriver,Java,Html,Selenium,Selenium Webdriver,我正在尝试获取选择器的下拉选项。当我使用Chrome访问网站,并使用F12查看HTML时,我处理的部分如下所示: <select name="routeSelector" onkeypress="selectorKeyPress(this, event); return false;" onfocus="clearKeyBuffer(this)" onchange="selected(this);routeSelected()"> <option value=

我正在尝试获取选择器的下拉选项。当我使用Chrome访问网站,并使用F12查看HTML时,我处理的部分如下所示:

<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>
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();
}
然而,当我使用SeleniumJava访问这个页面时,我得到

<select name="routeSelector" onkeypress="selectorKeyPress(this, event);return false;" onfocus="clearKeyBuffer(this)" onchange="selected(this);routeSelected()">
                          <option selected="selected">
                            _________________________
                          </option>
                        </select>
因此,我无法获得选项

我的代码如下所示:

<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>
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();
}
我还尝试过单击选择器,或者刷新页面,但这一切都不起作用。我猜这个网页上可能有一些Javascript

提前谢谢

- Java版本7
Selenium版本2

在阅读了该页面的源代码后,我解决了这个问题

因此,我从页面中找到了一段JavaScript代码:

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

当页面加载时,这段代码会被执行。如果要在selenium中执行此操作,必须手动执行这些代码。手动完成后,select.getOptions方法可能会返回选项。

我可以提供一个指向您正在测试的网页的链接。您是否可以尝试使用chromeDriver或firefoxDriver而不是HTMLUnit@Madhan谢谢你的建议!实际上,我已经解决了这个问题。我稍后再回答。