Switch tabs using Selenium WebDriver with Java

2018-12-31 20:21发布

Using Selenium WebDriver with JAVA , I am trying to automate a functionality where I have to open a new tab do some operations there and come back to previous tab (Parent). I used switch handle but its not working. And one strange thing the two tabs are having same window handle due to which i am not able to switch between tabs.

However when I am trying with different Firefox windows it works, but for tab its not working.

Please help me how can i switch tabs. OR how can I switch tabs without using window handle as window handle is same of both tabs in my case.

(I have observed that when you open different tabs in same window , window handle remains same.)

19条回答
浮光初槿花落
2楼-- · 2018-12-31 20:57

This will work for the MacOS for Firefox and Chrome:

// opens the default browser tab with the first webpage
driver.get("the url 1");
thread.sleep(2000);

// opens the second tab
driver.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND + "t");
driver.get("the url 2");
Thread.sleep(2000);

// comes back to the first tab
driver.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND, Keys.SHIFT, "{");
查看更多
余生无你
3楼-- · 2018-12-31 20:58
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL,Keys.SHIFT,Keys.TAB);

This method helps in switching between multiple windows. The restricting problem with this method is that it can only be used so many times until the required window is reached. Hope it helps.

查看更多
余生请多指教
4楼-- · 2018-12-31 20:59

Since the driver.window_handles is not in order , a better solution is this.

first switch to the first tab using the shortcut Control + X to switch to the 'x' th tab in the browser window .

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "1");
# goes to 1st tab

driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "4");
# goes to 4th tab if its exists or goes to last tab.
查看更多
回忆,回不去的记忆
5楼-- · 2018-12-31 20:59

I had a problem recently, the link was opened in a new tab, but selenium focused still on the initial tab.

I'm using Chromedriver and the only way to focus on a tab was for me to use switch_to_window().

Here's the Python code:

driver.switch_to_window(driver.window_handles[-1])

So the tip is to find out the name of the window handle you need, they are stored as list in

driver.window_handles
查看更多
路过你的时光
6楼-- · 2018-12-31 20:59

Simple Answer which worked for me:

for (String handle1 : driver1.getWindowHandles()) {
        System.out.println(handle1); 
        driver1.switchTo().window(handle1);     
}
查看更多
情到深处是孤独
7楼-- · 2018-12-31 21:00

Please see below:

WebDriver driver = new FirefoxDriver();

driver.manage().window().maximize();
driver.get("https://www.irctc.co.in/");
String oldTab = driver.getWindowHandle();

//For opening window in New Tab
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); 
driver.findElement(By.linkText("Hotels & Lounge")).sendKeys(selectLinkOpeninNewTab);

// Perform Ctrl + Tab to focus on new Tab window
new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, Keys.TAB)).perform();

// Switch driver control to focused tab window
driver.switchTo().window(oldTab);

driver.findElement(By.id("textfield")).sendKeys("bangalore");

Hope this is helpful!

查看更多
登录 后发表回答