0

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

这是我的 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">
4

1 回答 1

0

我不得不承认我并没有看清楚所有的代码,但这里有一些评论。

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

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

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

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

于 2013-08-21T19:00:04.327 回答