如何在网页上执行随机Javascript代码?
我正在使用 htmlunit 来测试一些页面,我想知道如何在当前页面的上下文中执行一些 javascript 代码。我知道文档说我最好模拟页面上用户的行为,但它不是这样工作的:((我有一个 div
,它有一个 onclick 属性,我调用其 click
方法,但没有任何反应,所以我进行了一些谷歌搜索并尝试:
JavaScriptEngine jse = webClient.getJavaScriptEngine();
jse.execute(page, what here?);
似乎我必须首先实例化脚本,但我没有找到有关如何执行此操作的信息() 。对吧)有人可以分享一个代码片段来展示如何让 webclient 实例执行所需的代码吗?
I'm using htmlunit to test some pages and I'd like to know how can I execute some javascript code in the context of the current page. I'm aware that the docs say I'd better emulate the behavior of a user on a page, but it isn't working this way :( (I have a div
which has an onclick property, I call its click
method but nothing happens). So I've made some googling and tried:
JavaScriptEngine jse = webClient.getJavaScriptEngine();
jse.execute(page, what here?);
Seems like I have to instantiate the script first, but I've found no info on how to do it (right). Could someone share a code snippet showing how to make webclient instance execute the needed code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要调用 在页面上执行 JavaScript(),而不是在
webClient
上。示例:
打印“Google”。 (我确信您会在那里放入一些更令人兴奋的代码。)
You need to call executeJavaScript() on the page, not on
webClient
.Example:
prints "Google". (I'm sure you'll have some more exciting code to put in there.)
我不知道您引用的
JavaScriptEngine
,也许这不是您想要的答案,但这听起来像是 Selenium IDE。I don't know the
JavaScriptEngine
you're quoting and maybe it's not the answer you want, but this sounds like a perfect case for Selenium IDE.在使用 HTMLUnit 后端的 TestPlan 中,谷歌示例是:
In TestPlan using the HTMLUnit backend the google example is: