selenium Web 驱动程序 - 切换到父窗口 [英] selenium web driver - switch to parent window

查看:42
本文介绍了selenium Web 驱动程序 - 切换到父窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 selenium Web 驱动程序.我打开了一个父窗口.单击链接后,新窗口将打开.我从列表中选择了一些值,这个窗口会自动关闭.现在我需要在我的父窗口中操作.我怎样才能做到这一点?我尝试了以下代码:

I use selenium Web Driver. I have opened a parent window. After I click on the link the new window opens. I choose some value from the list and this window automatically closes. Now I need to operate in my parent window. How can I do this? I tried the following code:

String HandleBefore = driver.getWindowHandle();
driver.findElement(By.xpath("...")).click();
 for (String Handle : driver.getWindowHandles()) {
        driver.switchTo().window(Handle);}
 driver.findElement(By.linkText("...")).click();
 driver.switchTo().window(HandleBefore);

虽然这不起作用.

推荐答案

在单击打开新窗口的链接之前尝试此操作:

Try this before you click the link that opens the new window:

parent_h = browser.current_window
# click on the link that opens a new window
handles = browser.window_handles # before the pop-up window closes
handles.remove(parent_h)
browser.switch_to_window(handles.pop())
# do stuff in the popup
# popup window closes
browser.switch_to_window(parent_h)
# and you're back

这篇关于selenium Web 驱动程序 - 切换到父窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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