【发布时间】:2020-06-16 23:38:20
【问题描述】:
试图在 iframe 中存在的元素上触发 JavaScript“更改”事件。以下是我的代码轨迹
// get the iframe window
var iframeDoc = document.getElementsByTagName("iframe")[0].contentWindow.document;
// get the target element
var targetElement = iframeDoc.querySelector("#some_id");
// add a listener to the target element to make sure the event is fired
targetElement.addEventListener("onchange", function(){console.log("change event triggered");});
// set value in the target element
targetElement.value = "abc";
// now build the custom event
var customEvent = iframeDoc.createEvent("HTMLEvents");
customEvent.intEvent("change", false, true);
// trigger the event on the element
targetElement.dispatchEvent(customEvent)
// now click on the save button
最后一行执行后我们没有收到任何错误,并在控制台中看到消息。但是,当单击表单中的Save 按钮时,selenium 输入的值不会保留。
技术栈: Java,IE 11,硒 3.141.59 禁用原生事件
【问题讨论】:
-
你能用相关的基于文本的 HTML 更新问题吗?
-
将尝试去识别数据,看看我是否可以发布示例代码。
-
@supputuri 你能补充一些关于表格的信息吗?
-
该页面依赖于不同的事件或期待受信任的事件。试试
element.dispatchEvent(new Event('input'))。在调度事件之前,我还会尝试使用 Selenium 将上下文切换到框架。 -
已尝试使用
input、change、blur、keydown、keypress和keyup事件,但仍然没有保存数据。我看到的不同之处在于,当我手动输入数据时,我看到使用 selenium 输入值时没有触发的少数突变事件。
标签: javascript selenium iframe