Selenium ChromeDriver switch tabs

2020-06-03 00:01发布

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:

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?

5条回答
戒情不戒烟
2楼-- · 2020-06-03 00:08

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.

//Rotate Tabs
seleniumDriver.SwitchTo().Window(seleniumDriver.WindowHandles[currentUrlIndex]);
IJavaScriptExecutor jscript = seleniumDriver as IJavaScriptExecutor;
jscript.ExecuteScript("alert('Focus')");
seleniumDriver.SwitchTo().Alert().Accept();
查看更多
Anthone
3楼-- · 2020-06-03 00:13

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
查看更多
狗以群分
4楼-- · 2020-06-03 00:15

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

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");
查看更多
趁早两清
5楼-- · 2020-06-03 00:23

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());
查看更多
smile是对你的礼貌
6楼-- · 2020-06-03 00:30

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.

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

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 :)

查看更多
登录 后发表回答