Selenium ChromeDriver switch tabs

前端 未结 5 730
走了就别回头了
走了就别回头了 2021-02-07 11:10

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

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 11: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
    

提交回复
热议问题