简体   繁体   中英

Can i switch between windows by its page title using selenium webdriver in java?

Found answers related to switching between windows using getDriver().getWindowHandles() and iterating through it.

But I would like to know if there is any way to switch between windows using page title in selenium webdriver using java.

Note: I am a newbie to Selenium Framework

Yes you can.

String your_title = "This is the Title";
String currentWindow = driver.getWindowHandle();  //will keep current window to switch back
for(String winHandle : driver.getWindowHandles()){
   if (driver.switchTo().window(winHandle).getTitle().equals(your_title)) {
     //This is the one you're looking for
     break;
   } 
   else {
      driver.switchTo().window(currentWindow);
   } 
}

If you have the exact title of the window you want to switch focus to (ie the exact content of the top-level <title> tag of the window), then you can simply do the following:

driver.switchTo().window("Whatever the title is, as a String");

If you only have the partial title of the window you want to switch focus to, then you should iterate over the available windows by their handles as the other answers suggest.

Reference: Code to switch back to parent window in case the window with title is not found.

    public void switchToParentWindowHandle() {
        driver.switchTo().window(parentWindowHandle);
    }

Reference: Code to get the original window handle in case the window with the title is not found.

    public void getParentWindowHandle() {
        parentWindowHandle = driver.getWindowHandle();
    }

Code that will try to switch based on the title of the window.

private String parentWindowHandle;

    public void switchToWindow(String title) {
        boolean foundWindow = false;
        getParentWindowHandle(); //See above reference

        for (String handle : driver.getWindowHandles()) {
            if (driver.switchTo().window(handle).getTitle().contains(title)) {
                System.out.println("Switched to window with title:" + title);
                foundWindow = true;
                break;
            }
        }
        if (foundWindow) {
            System.out.println("Couldn't find the window with title -> " + title +  "\nSwitching to parent window.");
            switchToParentWindowHandle(); //See above reference
        }
    }

Hi you can try like below

driver.get("http://www.seleniumhq.com");
// for understanding point please open a new tab
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL + "t");
// note new tab title will be blank as we have not opened anything in it.

// Store the current window handle- the parent one
String winHandleBefore = driver.getWindowHandle();
// store all the windows 
Set<String> handle= driver.getWindowHandles();
// iterate over window handles
for(String mywindows : handle){
// store window title
 String myTitle = driver.switchTo().window(mywindows).getTitle();
 // now apply the condition - moving to the window with blank title
  if(myTitle.equals("")){
  // perform some action - as here m openning a new url
  driver.get("http://docs.seleniumhq.org/download/");
}else{
    driver.switchTo().window(winHandleBefore);
}
}

Hope this helps what you are looking for.

if you are using xpath expressions like this:

$x("//*[@id=\"cbenef\"]").click();

Where the click() method opens a window. You can switch to that new window with the title of the page. The title is present in headers, like this:

 <!DOCTYPE html> <html> <head> <title>Amazing</title> </head> <body> <h1>Heading</h1> </body> </html>

In this case you can switch to this window using:

switchTo().window("Amazing");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM