我正在尝试使用 HtmlUnity,但在尝试使用 javaScript 单击区域时遇到了麻烦。
这是代码:
import java.io.IOException;
import java.net.MalformedURLException;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlArea;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlMap;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class ToPost {
/**
* @param args
* @throws IOException
* @throws MalformedURLException
* @throws FailingHttpStatusCodeException
*/
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
HtmlPage page;
final WebClient webClient = new WebClient();
page = webClient.getPage("http://www.hidrografico.pt/previsao-mares.php");
System.out.println(page.getTitleText());
HtmlPage pagePortoLeixoes = setPort(page, "362,64,440,90");
System.out.println("Are they the same? "+page.asXml().equals(pagePortoLeixoes.asXml()));
}
private static HtmlPage setPort(HtmlPage page, String coordinatesPort) throws IOException {
HtmlMap map = page.getHtmlElementById("FPMap1");
Iterable<HtmlElement> childAreas = map.getChildElements();
HtmlArea tempArea;
for (HtmlElement htmlElement : childAreas) {
tempArea = (HtmlArea) htmlElement;
if(tempArea.getCoordsAttribute().equals(coordinatesPort)){
System.out.println("Found Leixoes! --> "+ tempArea.asXml());
return tempArea.click();
}
}
return null;
}
}
我没有在这里显示它,但我仔细检查了我的完整代码,我真的不在我想要的页面中。
怎么了?为什么点击不起作用?