I'm trying to switch the tab and do operation by navigating to url'http://toolsqa.com/' and then to Demo Sites--> E-Commerce Demo Site. Once this link is clicked a new tab in the same browser with url 'http://store.demoqa.com/' is opened. Here i tried many ways for switching the focus to the newly opened tab. But unable to do it, getting unable to identify element error while trying to do any operations in new tab.
Different codes used:
code1:
public void switchWindow(String originalWindowKey){
System.out.println("Ori"+originalWindowKey);
Set<String> multipleWindowHandles = driver.getWindowHandles();
System.out.println(multipleWindowHandles);
System.out.println("Entry1");
String newWindowKey = "";
System.out.println("Entry2");
for (String windowKeys : multipleWindowHandles) {
System.out.println("1"+windowKeys);
if(windowKeys.equals(originalWindowKey) == false){
System.out.println("Success");
newWindowKey = windowKeys;
}
}
driver.switchTo().window(newWindowKey);
}
code2:
public void sampleSwitch(){
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
driver.switchTo().defaultContent();
}
Code3:
public void oneSwitch(){
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.PAGE_DOWN);
}
The above following ways i used for switching the tabs, but no luck yet. Please help me in this regard.