专注于 Selenium Webdriver 和 Python 中的新窗口 [英] Get focus on the new window in Selenium Webdriver and Python

查看:29
本文介绍了专注于 Selenium Webdriver 和 Python 中的新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中使用 Selenium Webdriver 我可以点击一个按钮来打开一个新的浏览器窗口,但我不知道如何将焦点转移到新窗口上.我在互联网上搜索了所有内容,但没有发现任何帮助.问题是窗口没有标题!

Using Selenium Webdriver in Python I am able to click on a button that opens a new browser window, but I have no idea how to change the focus onto the new window. I've searched all over the internet, but found nothing helpful. The problem is that the window doesn't have a title!

我需要的是新窗口的焦点,所以我可以截取其内容.

What I need is the focus on the new window, so I can take a screenshot of its contents.

这是打开新窗口的按钮周围的代码:

Here is the bit of code around the button that opens the new window:

怎么做...?

推荐答案

您将需要使用 .switchTo(windowHandle); 命令来访问您的第二个窗口.

You will need to use the .switchTo(windowHandle); command to access your second window.

在打开第二个窗口之前——获取打开窗口的windowHandle:

Before opening the second window - get the windowHandle of the open window:

String mainWindow = driver.getWindowHandle();

然后执行打开第二个窗口的操作.现在您需要知道第二个窗口的句柄并将控制切换到它:

Then do your action that opens the second window. Now you'll need to know the handle of the second windowand switch control to it:

Set<string> handles = driver.getWindowHandles();  
    for (String handle : handles) {
        if (!handler.equals(mainWindow)) {
            driver.switchTo(handle);
            break;
    }
}

您对第二个窗口的操作现在将在第二个窗口中发生.当您完成并需要再次与第一个窗口交互时:driver.switchTo().defaultContent();

Your actions for the second window will now happen in that second window. When you're finished and need to interact with the first window again: driver.switchTo().defaultContent();

这篇关于专注于 Selenium Webdriver 和 Python 中的新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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