问题描述
我正在尝试使用我自己的搜索栏制作谷歌地图克隆(学校项目)。我一直在玩最新版本的 jxbrowser。我正在尝试访问“doc”以使用 findById 将输入设置为 dom html,并使用“value”设置值。我知道它是如何在 v6 中设置的:
private void kButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FirsT:event_kButton2ActionPerformed
DOMDocument doc = browser.getDocument();
DOMElement address_element = doc.findElement(By.id("address"));
DOMElement search_element = doc.findElement(By.id("submit"));
DOMElement button = (DOMElement) search_element;
DOMInputElement address = (DOMInputElement) address_element;
address.setValue(searchbar.getText());
button.click();
但是我如何在 v7.15 中复制此代码? 我在 v7.15 中的代码
//How do i do browser.getDocument(); ?
browser.mainFrame().ifPresent(frame ->
frame.document().ifPresent(document -> {
String baseUri = document.baseUri();
}));
//这部分怎么写,不能解析documentElement
documentElement.findElementById("address").ifPresent(element ->
((InputElement) element).value(new address));
documentElement.findElementById("sumbit").ifPresent(element ->
((InputElement) element).click
/// 我如何点击按钮?
解决方法
browser.mainFrame().flatMap(Frame::document).ifPresent(document -> {
String baseUri = document.baseUri();
System.out.println(searchLocationA.getText());
System.out.println(searchLocationB.getText());
document.findElementById("departure").ifPresent(element -> ((InputElement) element).value(searchLocationA.getText()));
document.findElementById("destination").ifPresent(element -> ((InputElement) element).value(searchLocationB.getText()));
document.findElementById("submit").ifPresent(Node::click);
System.out.println("buttons work");
});
解决方案