简体   繁体   English

你如何使用Selenium在一个框架内执行javascript?

[英]How do you use Selenium to execute javascript within a frame?

I have a page (index.html) which has a frame: 我有一个页面(index.html)有一个框架:

<html>
<body>
<iframe src="otherPage.html" />
</body>
</html>

And the otherPage.html has the contents: 而otherPage.html的内容如下:

<html>
<head><link src="jquery-min.js" type="text/javascript" /></head>
<body><div id="main">Contents</div></body>
</html>

I am attempting to use the following selenium code on index.html: 我试图在index.html上使用以下selenium代码:

selenium.open("index.html");
selenium.selectFrame("//iframe");
selenium.getEval("window.jQuery('div[id=main]')");

However this fails miserably. 然而,这失败了。 It says that the jQuery object doesn't exist. 它说jQuery对象不存在。

If I attempt to execute the selenium test on the otherPage like so: 如果我尝试在otherPage上执行selenium测试,如下所示:

selenium.open("otherPage.html");
selenium.getEval("window.jQuery('div[id=main]')");

everything is hunky dory. 一切都是笨拙的海鲂。

Right now this is pseudo code, if people want me to make it compile I'll do that and put it on github. 现在这是伪代码,如果人们想让我编译它我会这样做并把它放在github上。

Selenium stores the Window Object within its own object so it can manipulate it better. Selenium将Window对象存储在自己的对象中,以便更好地操作它。 Once you move to the frame it should be like this. 一旦你移动到框架,它应该是这样的。

selenium.getEval("var window = this.browserbot.getUserWindow();window.jQuery('div[id=main]')");

And that should work for you 这应该对你有用

I have a tutorial on my site for this type of situation here 我有我的网站教程这种类型的情况在这里

You can use : 您可以使用 :

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:sendEvent('play');");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM