HtmlUnit Java - HtmlOption未正确选择选项

时间:2013-08-20 21:46:39

标签: java javascript htmlunit

我在网站上有一个下拉列表,当点击一个选项时,该列表使用javascript导航到新的配置文件。问题是,我不认为HtmlUnit的选项是点击下拉框中的选项,只选择它,所以它永远不会导航到新页面。我知道它正在找到正确的下拉元素,因为我已经在网页的源代码中确认了它。我也使用getOption()并使用下拉框的索引4,所以getOption(4),但这可能是一个问题?我也尝试过值和id,但这似乎是唯一不会给我错误的方法。我还添加了一个Thread.sleep,以确保它有时间导航到下一页,以防出现问题,但我认为它没有帮助。所以要把事情搞砸,我需要它来物理点击其中一个选项才能导航,但是click()似乎不起作用?

这是我的HtmlUnit代码:

public static void changeIntensity(String value) throws IOException {
    try {

        final WebClient webClient = new WebClient(BrowserVersion.CHROME);
        DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
        creds.addCredentials(Main.USERNAME, Main.PASSWORD);
        webClient.setCredentialsProvider(creds);
        final HtmlPage page = webClient.getPage("http://192.168.1.30/profiles.sht"); // nav to profiles
        System.out.println("Logged into AquaController.");
        HtmlSelect select = (HtmlSelect) page.getElementByName("profileSel"); // this is for sure correst, i checked in profiles.asp == also, i tried a diff one and it said out of range, so this is correct.
        System.out.println("Selected profile dropdown box.");
        HtmlOption option =  select.getOption(4); // select blue 20 (its profule #12) - no use getOptionByText
        select.setSelectedAttribute(option, true);
        System.out.println("Selected option.");
        System.out.println("Thread sleeping for 5");
        Thread.sleep(5000);
        System.out.println("Thread done sleeping for 5");
        HtmlInput pMin = page.getElementByName("profileLMin"); // min intensity - name =
        HtmlInput pMax = page.getElementByName("profileLMax"); // max intensity
        pMin.click();
        pMin.setValueAttribute(value);
        pMax.click();
        pMax.setValueAttribute(value);
        HtmlElement updateProfile = page.getElementByName("Update");
        updateProfile.click();
        System.out.println("finished");
    } 
    catch (InterruptedException e) {
        e.printStackTrace();
    }
}

这是相关的网站来源:

    <div id='title'>
<br>
<h2>Apex Profile Setup</h2>
<br>
</div>
<script type="text/javascript">
leafHeight = 220;
//if (window.addEventListener){
//  window.addEventListener("load", autoInit, false);
//}
//else if (window.attachEvent){
//  window.attachEvent("onload", autoInit);
//}

function autoInit()
{
    var selector = document.getElementsByName("profileTypeSel");
    showLeaf(1, selector[0].selectedIndex+1);
    init();
}

function updatepSelect() {
document.pSelect.submit("profiles.sht");
}


<P> 
<form name="pSelect" method="post" action="profiles.sht">Profile:&nbsp;
        <select class="tbflat" name="profileSel" onchange="updatepSelect();" ><option selected value="1">&nbsp;BlueUP (PF1)&nbsp;</option><option  value="2">&nbsp;BluePK (PF2)&nbsp;</option><option  value="3">&nbsp;BlueDN (PF3)&nbsp;</option><option  value="4">&nbsp;Moon (PF4)&nbsp;</option><option  value="5">&nbsp;WhiteUP (PF5)&nbsp;</option><option  value="6">&nbsp;WhitePK (PF6)&nbsp;</option><option  value="7">&nbsp;WhiteDN (PF7)&nbsp;</option><option  value="8">&nbsp;WaveLFT (PF8)&nbsp;</option><option  value="9">&nbsp;WaveRGT (PF9)&nbsp;</option><option  value="10">&nbsp;CalmLFT (PF10)&nbsp;</option><option  value="11">&nbsp;CalmRGT (PF11)&nbsp;</option><option  value="12">&nbsp;Blue20 (PF12)&nbsp;</option><option  value="13">&nbsp;White20 (PF13)&nbsp;</option><option  value="14">&nbsp;Blue30 (PF14)&nbsp;</option><option  value="15">&nbsp;White30 (PF15)&nbsp;</option><option  value="16">&nbsp;Blue40 (PF16)&nbsp;</option><option  value="17">&nbsp;White40 (PF17)&nbsp;</option><option  value="18">&nbsp;Blue50 (PF18)&nbsp;</option><option  value="19">&nbsp;White50 (PF19)&nbsp;</option><option  value="20">&nbsp;Blue60 (PF20)&nbsp;</option><option  value="21">&nbsp;White60 (PF21)&nbsp;</option><option  value="22">&nbsp;Blue70 (PF22)&nbsp;</option><option  value="23">&nbsp;White70 (PF23)&nbsp;</option><option  value="24">&nbsp;Blue80 (PF24)&nbsp;</option><option  value="25">&nbsp;White80 (PF25)&nbsp;</option><option  value="26">&nbsp;Blue90 (PF26)&nbsp;</option><option  value="27">&nbsp;White90 (PF27)&nbsp;</option><option  value="28">&nbsp;Blue100 (PF28)&nbsp;</option><option  value="29">&nbsp;White100 (PF29)&nbsp;</option><option  value="30">&nbsp;PF30 (PF30)&nbsp;</option><option  value="31">&nbsp;PFSDBlue (PF31)&nbsp;</option><option  value="32">&nbsp;PFSDWhit (PF32)&nbsp;</option></select>
    <input name="profileID" id="profileID" type="hidden" value="1">
    <input name="compact" type="hidden" id="compact" value="0">
    </form>
        <form name="pUpdate" method="post" action="/profiles.sht"><table border="1">
            <input name="compact" type="hidden" id="compact" value="0">
            <tr><td><P>Profile Name&nbsp;</td>
            <td><input class="tbflat" name="profileName" size="15" maxlength="25" tabindex="1" value="BlueUP"></td></tr>
            <tr><td>Control Type &nbsp;</td>
            <td><select class="tbflat" name="profileTypeSel" onChange="showLeaf(1, this.options[this.selectedIndex].value)"><option  value="1">&nbsp;Pump&nbsp;</option><option selected value="2">&nbsp;Ramp&nbsp;</option><option  value="3">&nbsp;Vortech&nbsp;</option><option  value="4">&nbsp;Weather&nbsp;</option>
            </select></td></tr>
            <tr><td colspan="2">

1 个答案:

答案 0 :(得分:0)

我必须承认我并没有完全看到所有的代码,但这里有一些评论。

我相信HtmlUnit确实在选择该选项。通过“选择”我的意思是它将表单的选择字段设置为所选值。

现在,这里棘手的部分是你已经将一段javascript附加到onchange事件中。说到JS ...... HtmlUnit是不可预测的。这取决于很多因素,但根据我的经验,使用BrowserVersion的不同值有很大帮助。

此外,您应该考虑到您所谈论的click事件可能无法触发onchange。我会尝试手动触发该方法。

调试提示:尝试执行您期望在WebClient中触发onchange事件的JS,并查看是否提供了预期结果。