我在一个网站上有一个下拉列表,当单击一个选项时,它使用 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:
<select class="tbflat" name="profileSel" onchange="updatepSelect();" ><option selected value="1"> BlueUP (PF1) </option><option value="2"> BluePK (PF2) </option><option value="3"> BlueDN (PF3) </option><option value="4"> Moon (PF4) </option><option value="5"> WhiteUP (PF5) </option><option value="6"> WhitePK (PF6) </option><option value="7"> WhiteDN (PF7) </option><option value="8"> WaveLFT (PF8) </option><option value="9"> WaveRGT (PF9) </option><option value="10"> CalmLFT (PF10) </option><option value="11"> CalmRGT (PF11) </option><option value="12"> Blue20 (PF12) </option><option value="13"> White20 (PF13) </option><option value="14"> Blue30 (PF14) </option><option value="15"> White30 (PF15) </option><option value="16"> Blue40 (PF16) </option><option value="17"> White40 (PF17) </option><option value="18"> Blue50 (PF18) </option><option value="19"> White50 (PF19) </option><option value="20"> Blue60 (PF20) </option><option value="21"> White60 (PF21) </option><option value="22"> Blue70 (PF22) </option><option value="23"> White70 (PF23) </option><option value="24"> Blue80 (PF24) </option><option value="25"> White80 (PF25) </option><option value="26"> Blue90 (PF26) </option><option value="27"> White90 (PF27) </option><option value="28"> Blue100 (PF28) </option><option value="29"> White100 (PF29) </option><option value="30"> PF30 (PF30) </option><option value="31"> PFSDBlue (PF31) </option><option value="32"> PFSDWhit (PF32) </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 </td>
<td><input class="tbflat" name="profileName" size="15" maxlength="25" tabindex="1" value="BlueUP"></td></tr>
<tr><td>Control Type </td>
<td><select class="tbflat" name="profileTypeSel" onChange="showLeaf(1, this.options[this.selectedIndex].value)"><option value="1"> Pump </option><option selected value="2"> Ramp </option><option value="3"> Vortech </option><option value="4"> Weather </option>
</select></td></tr>
<tr><td colspan="2">