如何使用带有Java的Selenium WebDriver切换到另一个选项卡 [英] How to switch to another Tab using Selenium WebDriver with Java

查看:472
本文介绍了如何使用带有Java的Selenium WebDriver切换到另一个选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打开google.com,然后单击"GMail"超链接,并在同一浏览器上打开一个新标签.

I am opening google.com and then clicking on "GMail" hyperlink a new tab is opened on the same browser.

现在,我想切换到使用Selenium WebDriver打开GMail的新标签.

Now I want to switch to this new tab where GMail is opened using Selenium WebDriver.

代码段是:

  WebDriver wd = new ChromeDriver();
  wd.get("https://www.google.co.in/?gws_rd=ssl");       
  wd.findElement(By.linkText("Gmail")).sendKeys(Keys.CONTROL,Keys.RETURN);

现在,我要转到打开GMail链接的标签.我已经搜索了N个解决方案,但没有一个起作用.

Now I want to go to the tab where I have opened GMail link. I have googled through N number of solutions but none of them worked.

例如

解决方案1:

String Tab1 = wd.getWindowHandle(); 
ArrayList<String> availableWindows = new ArrayList<String>(wd.getWindowHandles()); 
if (!availableWindows.isEmpty()) { 
wd.switchTo().window(availableWindows.get(1)); 
}

解决方案2:

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

建议.我被困住了.

推荐答案

对我来说,driver.getWindowHandles()对选项卡不起作用...您将始终使用CURRENT选项卡的句柄返回一个单项数组. .. [已删除旧答案,因为现在似乎已解决]

Seems to me that driver.getWindowHandles() does not work for tabs... you'll always get back a one item array with the CURRENT tab's handle... [deleted old answer as this seems to be fixed now]

更新(6/16/2016):Selenium的当前版本(服务器独立版2.53.0)的行为似乎有所不同.现在,我正在使用类似的方法将驱动程序打开/切换到新选项卡:

UPDATE (6/16/2016): the current version of Selenium (server standalone 2.53.0) seems to act differently. Now I'm using something like this to open/switch the driver to the new tab:

更新(11/16/2016):硒3.0.1似乎又改变了事情?我必须使用javascript现在打开一个新标签.这似乎仅在Chrome中有效.Firefox打开一个新窗口.我将查看是否可以使用geckodriver的设置(功能/配置文件?)更改该行为.

UPDATE (11/16/2016): Selenium 3.0.1 seems to have changed things again? I have to use javascript to open a new tab now. This seems to work in Chrome only... Firefox opens a new window. I'm going to see if that behavior can be changed using geckodriver's settings (capabilities/profile?).

// Actions actions = new Actions(driver); 
// actions.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();

((JavascriptExecutor)driver).executeScript("window.open('about:blank', '_blank');");

Set<String> tab_handles = driver.getWindowHandles();
    int number_of_tabs = tab_handles.size();
    int new_tab_index = number_of_tabs-1;
    driver.switchTo().window(tab_handles.toArray()[new_tab_index].toString()); 

getWindowHandles()方法现在返回一个Set.到目前为止,这仅在Chrome中进行了测试,因为Firefox 47当前在使用firefoxdriver ...和geckodriver时存在一些严重问题,因此Actions根本无法正常工作. [2016年6月11日更新]:通过geckodriver的Firefox现在返回一个Set]

the getWindowHandles() method is now returning a Set. This has only been tested in Chrome so far, since Firefox version 47 currently has some serious problems using the firefoxdriver... and using geckodriver the Actions aren't working at all. [update 6/11/2016]: Firefox through geckodriver now returns a Set]

您还可以执行以下操作:将其强制转换为ArrayList:

You can also do something like this.. cast it to ArrayList:

  //  set tab_index to the number of window/tab you want.  0 is the first tab 

       ArrayList<String> tabs_windows = new ArrayList<String> (driver.getWindowHandles());
    driver.switchTo().window(tabs_windows.get(tab_index));

更新:要解决geckodriver错误,我已改用element.sendkeys ...在Marionette和Chrome中似乎可以使用这种方法.(Update2):由于Selenium 3.0的更改,已更新为javascript. 1:

Update: To get around the geckodriver bug, I've switched to using element.sendkeys... something like this seems to work in Marionette and Chrome.. (Update2): Updated to javascript because of changes in Selenium 3.0.1:

// driver.findElement(By.cssSelector("body")).sendKeys(Keys.chord(Keys.CONTROL, "t"));

((JavascriptExecutor)driver).executeScript("window.open('about:blank', '_blank');");

    Set<String> tab_handles = driver.getWindowHandles();
    int number_of_tabs = tab_handles.size();
    int new_tab_index = number_of_tabs-1;
    driver.switchTo().window(tab_handles.toArray()[new_tab_index].toString()); 

更新(11/16/2016):使用Ctrl-W关闭的旧方法似乎也坏了...使用此方法:

UPDATE (11/16/2016): the old method to close using Ctrl-W seems to be broken, too... use this:

((JavascriptExecutor)driver).executeScript("close();");

这篇关于如何使用带有Java的Selenium WebDriver切换到另一个选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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