如何使用Selenium WebDriver在后台打开chrome新标签页窗口? [英] How can i use Selenium WebDriver to open chrome new tab window in the background?

查看:1255
本文介绍了如何使用Selenium WebDriver在后台打开chrome新标签页窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我仅使用Process Start,它将打开一个新的浏览器镶边窗口,但将焦点放在该窗口上. 我想打开一个新的chrome标签,但是如果没有重点关注,该标签将位于我当前所在的当前窗口后面的背景中.

If i'm using just Process Start it will open a new browser chrome window but will focus on the window. I want to open a new chrome tab but without focus on it the tab will be on the background behind the current window i'm in now.

仅当我单击浏览器图标时,我才会看到打开的选项卡,并且可以单击该选项卡将其聚焦.

Only if i click on the browser icon i will see the opened tab and can click on the tab to focus on it.

在Form1顶部,我做了:

In Form1 top i did:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

构造函数:

OpenBroswerTab();

还有

private void OpenBroswerTab()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl("http://stackoverflow.com/");

            IWebElement body = driver.FindElement(By.TagName("body"));
            body.SendKeys(OpenQA.Selenium.Keys.Control.ToString() + 't');

            //driver.Quit();
        }

但是使用此代码,我仍然看到chrome打开的新标签页/窗口. 新打开的标签/窗口应在后台最小化打开,这样我就看不到它打开.

But also with this code i still see the opened new tab/window of the chrome. The new opened tab/window should be minimized opened in the background so i will not see it open.

推荐答案

您可以使用窗口句柄来控制选项卡:

You can use the window handles to control the tabs:

如果您打开了一个标签页,然后又打开了另一个标签页,则可以立即调用此标签以返回到先前打开的标签页.如果您打开了许多标签,并且只想回到第一个标签,则应该可以将"myDriver.WindowHandles.Count -1"替换为简单的"0".

If you have a tab open and you open another one, you can immediately call this to return to the previously open tab. If you have many tabs open and just want to get back to the first one you should be able to replace the "myDriver.WindowHandles.Count -1" with simply "0".

ChromeDriver myDriver = new ChromeDriver();
myDriver.SwitchTo().Window(myDriver.WindowHandles[myDriver.WindowHandles.Count - 1]);

这篇关于如何使用Selenium WebDriver在后台打开chrome新标签页窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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