你如何使用Selenium在一个框架内执行javascript? [英] How do you use Selenium to execute javascript within a frame?
问题描述
我有一个页面(index.html),它有一个框架:
I have a page (index.html) which has a frame:
<html>
<body>
<iframe src="otherPage.html" />
</body>
</html>
而otherPage.html的内容为:
And the otherPage.html has the contents:
<html>
<head><link src="jquery-min.js" type="text/javascript" /></head>
<body><div id="main">Contents</div></body>
</html>
我试图在index.html上使用以下selenium代码:
I am attempting to use the following selenium code on index.html:
selenium.open("index.html");
selenium.selectFrame("//iframe");
selenium.getEval("window.jQuery('div[id=main]')");
然而这种情况很糟糕。它说jQuery对象不存在。
However this fails miserably. It says that the jQuery object doesn't exist.
如果我尝试在其他页面上执行selenium测试,如下所示:
If I attempt to execute the selenium test on the otherPage like so:
selenium.open("otherPage.html");
selenium.getEval("window.jQuery('div[id=main]')");
一切都很笨拙。
对现在这是伪代码,如果人们想让我编译它我会这样做并把它放在github上。
Right now this is pseudo code, if people want me to make it compile I'll do that and put it on github.
推荐答案
Selenium将Window对象存储在自己的对象中,以便更好地操作它。一旦你移动到框架,它应该是这样的。
Selenium stores the Window Object within its own object so it can manipulate it better. Once you move to the frame it should be like this.
selenium.getEval("var window = this.browserbot.getUserWindow();window.jQuery('div[id=main]')");
这应该对你有用
我在我的网站上有一个针对此类情况的教程此处
I have a tutorial on my site for this type of situation here
这篇关于你如何使用Selenium在一个框架内执行javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!