何时以及如何在webDriver(Selenium 2)中使用switchTo()方法来处理窗口/弹出窗口 [英] When and how to use switchTo() method in webDriver (Selenium 2) to handle window/pop up

查看:401
本文介绍了何时以及如何在webDriver(Selenium 2)中使用switchTo()方法来处理窗口/弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单击某些页面元素或与之交互时遇到麻烦.它们看起来像普通的DOM对象.只是这里的按钮是一个代码

I am having trouble clicking or interacting with some page elements. They seem like normal DOM objects. just buttons here is the code for one

<div style="clear: both; text-align: right">
 <img id="nextstep" class="nextbtn" height="21" alt="" src="/edit/images/author/next.png">
</div>

单击上传文件"按钮后,另一个弹出窗口打开.我的任务是在弹出窗口中上传文件,然后返回原始窗口.我会尽力解释

After clicking a button as "Upload file" another pop up window was opened. My task is to upload a file at pop up window and back to original window. I'll try to explain

我为以下应用程序启动了一个webDriver

I start a webDriver for

www.test.com(例如-组成名称)

www.test.com (<- made up name for example)

我登录

点击上传文件按钮

点击此按钮后,我们的网站将重定向到www.foo.com而不是上面的网站,我使用的是之前创建的相同驱动程序,我尝试与帖子上传页面上的按钮进行交互

After this click our site redirects to www.foo.com instead of the above I am using the same driver created earlier and I try to interact with the buttons on the post upload page

button.click()

button.click()

它从未找到这些按钮,但是它们位于萤火虫中,并且显示正常.

It never finds these buttons but they are there in firebug and appear normal.

推荐答案

您可以在重定向后尝试再次切换到窗口.

You may try to switch to the window again after the redirect.

为您保存窗口句柄:

String myWindowHandle = driver.getWindowHandle();

重定向到所需的页面,然后使用窗口句柄调用switchTo:

redirect to the page you want and then call switchTo using the window handle:

driver.switchTo().window(myWindowHandle );

您的页面是否包含任何框架?重定向后,您可能还不在正确的帧中.

Does your page contain any frames? It may also be possible that you are not in the correct frame after the redirect.

这篇关于何时以及如何在webDriver(Selenium 2)中使用switchTo()方法来处理窗口/弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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