有没有办法通过量角器或WebDriver物理关闭选项卡?
我之所以这样问,是因为虽然我知道如何以编程方式使用switch tabs,但它不会将活动选项卡带到前台。我不能总是知道在SauceLabs上运行的E2E测试中发生了什么,因为当我查看屏幕投射时,它显示的是我导航离开的选项卡,而不是活动的选项卡。
我是不是说错了?
it('should do something in the previous tab', function(done) {
browser.getAllWindowHandles().then(function (handles) {
browser.switchTo().window(handles[0]);
// do something
expect(something).toEqual(thisThing);
done();
});
});
发布于 2017-12-18 19:10:51
Sakshi答案的C#版本:
var tabs = driver.WindowHandles;
if (tabs.Count > 1)
{
driver.SwitchTo().Window(tabs[1]);
driver.Close();
driver.SwitchTo().Window(tabs[0]);
}
发布于 2018-02-13 08:41:06
关闭除第一个选项卡之外的所有选项卡,并切换到第一个选项卡:
var tabs = driver.WindowHandles; //
foreach (var tab in tabs)
{
// "tab" is a string like "CDwindow-6E793DA3E15E2AB5D6AE36A05344C68"
if (tabs[0] != tab)
{
driver.SwitchTo().Window(tab);
driver.Close();
}
}
driver.SwitchTab(tabs[0]); // Switch to first tab
driver.SwitchTo().DefaultContent(); // Switch to default frame
注意最后两行,它们需要避免像OpenQA.Selenium.NoSuchWindowException: no such window: target window already closed from unknown error: web view not found
这样的错误
发布于 2017-12-27 13:35:02
您可以使用driver.close
,然后切换到活动选项卡driver.SwitchTo().Window(m_driver.WindowHandles.First());
或任何其他可用选项卡
https://stackoverflow.com/questions/29502255
复制