如何切换到新标签页,在子标签页中执行一些操作,然后返回到父标签页?
在下面的代码中,将其转到新标签页,执行操作。但是父选项卡(yahoo)的会话与子选项卡(gmail)的会话相同
这是我的代码:
driver.get("http://www.yahoo.com");
ReadExcell.setExcelFile(Constant.filepath + Constant.fileName, "Sheet1");
System.out.println(driver.getTitle());
String parentwindow=driver.getWindowHandle();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.open('http://www.gmail.com','_blank');");
Set<String> s1=driver.getWindowHandles();
System.out.println("windows count is " +s1.size());
for (String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
String str=winHandle.toString();
System.out.println("The tostring value is" +str);
driver.get("http://www.gmail.com");
String Email=ReadExcell.getCellData(10, 1);
String Pwd=ReadExcell.getCellData(10, 2);
GmailLogin.Emailid(driver).sendKeys(Email);
GmailLogin.next(driver).click();
GmailLogin.Password(driver).sendKeys(Pwd);
GmailLogin.Pwdnext(driver).click();
System.out.println(driver.getTitle());
//System.out.println("Window count is" +winHandle());
}
driver.close();
driver.switchTo().window(parentwindow);
System.out.println(parentwindow);
System.out.println("Ater switching title is ="+driver.getTitle());
还有其他方法来命名每个选项卡并转到相应的选项卡执行一些操作并返回到父选项卡吗?您的帮助将更加有帮助...。 注意:我使用的是Mac
答案 0 :(得分:0)
您可以使用此方法:
public void switchToWindow(int index){
Set<String> windows = driver.getWindowHandles();
driver.switchTo().window((String) windows.toArray()[index]);
}
用法:
switchToWindow(1); // Switch to new window
doSomething();
switchToWindow(0); // Switch back to original window