Selenium WebDriver Java:在 DOMContentLoaded 之前更改 DOM
我的目标是在 DOMContentLoaded 事件之前更改页面的 DOM。假设我的 JS 看起来像下面的代码,我想更改元素的值:
document.addEventListener("DOMContentLoaded", function(event) {
console.log("Value of element foo: " + document.getElementById('foo').value);
});
我知道使用 Selenium WebDriver 的 JavascriptExecutor 更改 DOM,但我不知道如何获取它to 在“DOMContentLoaded”之前执行(也许这不是正确的方法)。
// some hook or whatever to execute right before 'DOMContentLoaded' or wherever suitable
((JavascriptExecutor) webDriver).executeScript("document.getElementById('foo').value='hi there'");
My aim is to change the DOM of a page before the DOMContentLoaded event. Let's say my JS would look like the code below and I want to change a value of an element:
document.addEventListener("DOMContentLoaded", function(event) {
console.log("Value of element foo: " + document.getElementById('foo').value);
});
I know to change the DOM with the JavascriptExecutor
of Selenium WebDriver, but I'm missing how to get it to to execute right before 'DOMContentLoaded' (and maybe that's not the right approach).
// some hook or whatever to execute right before 'DOMContentLoaded' or wherever suitable
((JavascriptExecutor) webDriver).executeScript("document.getElementById('foo').value='hi there'");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您必须设置
pageLoadStrategy
从默认正常
到无
。这将在使用
driver.get()
方法启动页面后立即将控制权传递到下一个代码行,而无需等待页面内容加载。In order to do that you will have to set the
pageLoadStrategy
from defaultnormal
tonone
.This will pass the control to the next code line immediately after launching the page with
driver.get()
method without waiting for the page content to be loaded.