C# Selenium ChromeDriver 开关选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10550031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:16:30  来源:igfitidea点击:

Selenium ChromeDriver switch tabs

c#seleniumselenium-chromedriver

提问by Lisa Young

When I click on a link in my test, it opens a new tab. I want ChromeDriver to then focus on that tab. I have tried the following code to get ChromeDriver to change tabas using the ctrl+tab shortcut:

当我单击测试中的链接时,它会打开一个新选项卡。我希望 ChromeDriver 然后专注于该选项卡。我已尝试使用以下代码让 ChromeDriver 使用 ctrl+tab 快捷方式更改 tabas:

Actions builder = new Actions(driver);
builder.KeyDown(Keys.Control).KeyDown(Keys.Tab).KeyUp(Keys.Tab).KeyUp(Keys.Control);//switch tabs
IAction switchTabs = builder.Build();
switchTabs.Perform();

But this throws the following exception:

但这会引发以下异常:

ekmLiveChat.tests.UITests.EndToEndTest.EndToEnd:
System.ArgumentException : key must be a modifier key (Keys.Shift, Keys.Control, or Keys.Alt)
Parameter name: key

Is there a way to switch tabs using ChromeDriver?

有没有办法使用 ChromeDriver 切换标签页?

采纳答案by Torbj?rn Kalin

As mentioned in my comment on your post, I'm not sure if the Chrome driver handles tabs the same way as it handles windows.

正如我在您的帖子中的评论中所提到的,我不确定 Chrome 驱动程序处理选项卡的方式是否与处理窗口的方式相同。

This code works in Firefox when opening new windows, so hopefully it works in your case as well:

此代码在打开新窗口时适用于 Firefox,因此希望它也适用于您的情况:

public void SwitchToWindow(Expression<Func<IWebDriver, bool>> predicateExp)
{
    var predicate = predicateExp.Compile();
    foreach (var handle in driver.WindowHandles)
    {
        driver.SwitchTo().Window(handle);
        if (predicate(driver))
        {
            return;
        }
    }

    throw new ArgumentException(string.Format("Unable to find window with condition: '{0}'", predicateExp.Body));
}

SwitchToWindow(driver => driver.Title == "Title of your new tab");

(I hope my edits to the code for this answer didn't introduce any errors...)

(我希望我对这个答案的代码所做的编辑没有引入任何错误......)

Just make sure you don't start looking for the new tab before Chrome has had the chance to open it :)

只要确保在 Chrome 有机会打开它之前不要开始寻找新标签:)

回答by DevDav

This is what worked for me:

这对我有用:

var popup = driver.WindowHandles[1]; // handler for the new tab
Assert.IsTrue(!string.IsNullOrEmpty(popup)); // tab was opened
Assert.AreEqual(driver.SwitchTo().Window(popup).Url, "http://blah"); // url is OK  
driver.SwitchTo().Window(driver.WindowHandles[1]).Close(); // close the tab
driver.SwitchTo().Window(driver.WindowHandles[0]); // get back to the main window

回答by Henry Wood

After a long fight with this I was able to get this working with chrome driver. The alert message is not visible but brings tab to front and accept closes it immediately.

经过长时间的斗争,我能够使用 chrome 驱动程序来实现它。警报消息不可见,但将选项卡放在前面并接受立即关闭它。

//Rotate Tabs
seleniumDriver.SwitchTo().Window(seleniumDriver.WindowHandles[currentUrlIndex]);
IJavaScriptExecutor jscript = seleniumDriver as IJavaScriptExecutor;
jscript.ExecuteScript("alert('Focus')");
seleniumDriver.SwitchTo().Alert().Accept();

回答by mani kandan

In C# I used the below lines to switch between the two tab.

在 C# 中,我使用以下几行在两个选项卡之间切换。

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;   
js.ExecuteScript("window.open();");   
IList<string> tabs = new List<string>(driver.WindowHandles);    
driver.SwitchTo().Window(tabs[1]);
driver.Navigate().GoToUrl("http://www.google.com");

回答by Betochas

On my code I click a button and opens a tab (so it is already on the new tab, I don't need to do something to go to that new tab) and run this so it recognize the new tab and worked:

在我的代码中,我单击一个按钮并打开一个选项卡(因此它已经在新选项卡上,我不需要执行任何操作即可转到该新选项卡)并运行它,以便它识别新选项卡并工作:

driver.SwitchTo().Window(driver.WindowHandles.Last());