- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这个website包含以下 html 定义:
<input class="startDateInput" id="startdate" name="startdate" type="text" value="09/21/2015" maxlength="10" size="10" onkeypress="return isDateKey(event)" onblur="if (this.value.length!=10) { this.value='09/21/2015'; }">
<a id="retrieve" href="javascript:void(0);" onclick="doSubmit('Retrieve')">Retrieve Share Prices</a>
使用 javafx,可以使用
更新开始日期字段int startDateIndx = ...;
String newStartDate = ...;
String setStartDate = "document.getElementsByTagName('input')["
+startDateIndx+"].value='" + newStartDate + "';";
webEngine.executeScript(setStartDate);
但是,如何在 java 中执行“检索股票价格”元素的点击?
这次尝试:
HTMLInputElement doRetrieve = (HTMLInputElement) doc.getElementById("retrieve");
doRetrieve.click();
生成此错误:
com.sun.webkit.dom.HTMLAnchorElementImpl cannot be cast to org.w3c.dom.html.HTMLInputElement
这是一个演示代码
public class subForms extends Application {
public static void main(String[] args) {
launch(args);
}
private boolean notEmpty(String s) {
return s != null && !"".equals(s);
}
@Override
public void start(Stage stage) {
final TextField fxUsername = new TextField();
final TextField fxPassword = new PasswordField();
final TextField fxStartDate = new TextField();
final BooleanProperty loginAttempted = new SimpleBooleanProperty(false);
final WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
webEngine.documentProperty().addListener(new ChangeListener<Document>() {
@Override
public void changed(ObservableValue<? extends Document> ov, Document oldDoc, Document doc) {
if (doc != null && !loginAttempted.get()) {
if (doc.getElementsByTagName("input").getLength() > 0) {
int startDateIndx = getInputIndex(doc, "startdate");
if (startDateIndx >= 0) {
// webEngine.executeScript("window.coordinates.lat =
// 31.2;");
String newStartDate = fxStartDate.getText();
String setStartDate = "document.getElementsByTagName('input')[" + startDateIndx
+ "].value='" + newStartDate + "';";
loginAttempted.set(true);
webEngine.executeScript(setStartDate);
// com.sun.webkit.dom.HTMLAnchorElementImpl cannot
// be cast to org.w3c.dom.html.HTMLInputElement
((HTMLInputElement) doc.getElementById("retrieve")).click();
}
}
}
}
private int getInputIndex(Document doc, String matchName) {
// matchName = "startdate"
NodeList htmlInputs = doc.getElementsByTagName("input");
for (int indx = 0; indx < htmlInputs.getLength(); indx++) {
Node inNode = htmlInputs.item(indx);
NamedNodeMap attributes = inNode.getAttributes();
// iterate over attributes
for (int i = 0; i < attributes.getLength(); i++) {
Attr attr = (Attr) attributes.item(i);
String attrName = attr.getNodeName();
if (attrName.equalsIgnoreCase("name")) {
String attrValue = attr.getNodeValue();
if (attrValue.equalsIgnoreCase(matchName)) {
return indx;
}
}
}
}
return -1;
}
});
webEngine.getLoadWorker().exceptionProperty().addListener(new ChangeListener<Throwable>() {
@Override
public void changed(ObservableValue<? extends Throwable> ov, Throwable oldException, Throwable exception) {
System.out.println("Load Exception: " + exception);
}
});
GridPane inputGrid = new GridPane();
inputGrid.setHgap(10);
inputGrid.setVgap(10);
Button fxButton = null;
fxStartDate.setText("3/3/2003");
inputGrid.addRow(0, new Label("Start Date: "), fxStartDate);
fxButton = new Button("Get TSP Share Price History");
fxButton.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
if (notEmpty(fxStartDate.getText())) {
loginAttempted.set(false);
final String tspURL = "https://www.tsp.gov/InvestmentFunds/FundPerformance/index.html";
webEngine.load(tspURL);
}
}
});
final VBox layout = new VBox(10);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 10;");
layout.getChildren().addAll(new Label("Enter start date for TSP share price history"),
inputGrid, fxButton, webView);
stage.setScene(new Scene(layout));
stage.show();
}
}
最佳答案
对于您发布的代码,单击链接只需调用 Javascript 函数 doSubmit('Retrieve')
。因此,对于这个特定的用例,您可以通过调用来达到您想要的效果
webEngine.executeScript("doSubmit('Retrieve')");
但这并不能回答您的实际问题。我怀疑通过正确的向下转换,可以强制单击 HTML 元素;但是相关的 DOM 类不是公共(public) API。
关于java - 如何从 javafx 单击 HTMLAnchorElement Impl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33382433/
a=document.createElement('a') a.port="123" "123" a.port "" 有什么方法可以更改端口?我发现 HTMLAnchorElement 正在使用
是否有一种 javascript 方法可以确定对象是否实现了“HTMLAnchorElement”接口(interface)?当我执行 typeOf(someVariable) 时,它返回 'obje
我想在每个菜单项前面添加浏览器中存在的 URL(由于某些 url 重定向内容而必须这样做)。但它在下面提到的行中给出了错误。我究竟做错了什么?正确的做法是什么? $(window)
升级到 Firefox 4 后,我开始在我维护的网站上看到此错误: Permission denied for to call method HTMLAnchorElement.focus 我构建了
我尝试使用 jquery 显示模态,但无济于事。 我试图重新安排我的 jquery 位置并研究其他问题,但我找不到任何问题的答案。 这是按钮 Tambah Mahasiswa 这是我放置脚
这个website包含以下 html 定义: Retrieve Share Prices 使用 javafx,可以使用 更新开始日期字段 int startDateIndx = ...; Strin
我有以下 HTML 方案: Video 14
Crime Thriller Drama
我在 jquery.Ajax 函数中操作 json 数组。语法让我很困惑,所以我找不到这个错误。我在做什么 我将点击功能绑定(bind)到 anchor 标记,然后当我点击该 anchor 标记并调用
如果我想检查特定的 DOM 节点是否是 anchor 元素,哪个选项会更安全、更快? 就更安全而言,我希望了解 IE6-8 等旧版浏览器能更好地支持哪种浏览器。 我假设速度不会有太大差异,但如果一个函
我有 Uncaught ReferenceError: previous is not defined at HTMLAnchorElement.onclick 请帮助我,我已经尝试过之前针对同一问题
我正在尝试为主页 slider 创建一个程式化的导航,但出现了一些控制台错误 - 即使脚本在大多数情况下都在执行我想要的操作。那是在鼠标悬停在焦点元素上时向相邻的同级元素添加类。就像笔显示的那样,它在
我正在 Chrome 中处理一个包含两个 Bootstrap 3 模态的页面。在您打开开发人员控制台并启动其中一个模式之前,一切看起来都很好。你可以用这个 jsfiddle 重复这个问题,再用最新的
我是一名优秀的程序员,十分优秀!