java 选项卡 切换界面,如何使用带有Java的Selenium WebDriver切换到另一个选项卡

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

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

Code snippet is :

WebDriver wd = new ChromeDriver();

wd.get("https://www.google.co.in/?gws_rd=ssl");

wd.findElement(By.linkText("Gmail")).sendKeys(Keys.CONTROL,Keys.RETURN);

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.

For e.g.

Solution 1 :

String Tab1 = wd.getWindowHandle();

ArrayList availableWindows = new ArrayList(wd.getWindowHandles());

if (!availableWindows.isEmpty()) {

wd.switchTo().window(availableWindows.get(1));

}

Solution 2 :

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

Kindly suggest. I am stuck on this.

解决方案

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]

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:

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 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());

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]

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 tabs_windows = new ArrayList (driver.getWindowHandles());

driver.switchTo().window(tabs_windows.get(tab_index));

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 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());

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值