硒:调用页面上的javascript函数 [英] Selenium : Call javascript function on page

查看:70
本文介绍了硒:调用页面上的javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在页面上调用现有的javascript函数,但没有得到.

I'm trying to call an existing javascript function on the page but I'm not getting it.

这是我拥有的元素:

<area shape="rect" coords="513,0,580,66" href="#" onclick="document.f2.submit();">

这是电话:

IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("document.f2.submit()");

例外:

unknown error: Cannot read property 'submit' of undefined
(Session info: chrome=55.0.2883.87)
(Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 6.1.7601 SP1 x86_64)

如何正常执行此操作?

推荐答案

有时会发生脚本执行时间过早并且页面中的某些元素尚未放置到位(从而产生错误)的情况.

It sometimes happens that the script executes too early and some elements in the page are not in place yet (thus giving errors).

在这些情况下,最好的办法是在一段时间后执行脚本,以确定这确实是问题所在.

The best thing to do in these cases is to execute the script after some time has passed in order to determine that that is really the problem.

js.ExecuteScript("setTimeout(function() { document.f2.submit(); }, 1000);");

在确认问题确实是时间之后,下一步是找出何时执行脚本注入,以便该元素在页面中并且没有问题(因此您可以从脚本注入中删除setTimeout.并仅保留您最初想要执行的操作.

After confirming that the problem really is timing, the next thing to do is figure out when to perform the script injection so that the element is in the page and there's no problem (and thus you can remove the setTimeout from the script injection and leave only the operation you wanted to perform initially).

这篇关于硒:调用页面上的javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆