简体   繁体   English

我可以在 java 中使用 selenium webdriver 通过页面标题在 windows 之间切换吗?

[英]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.找到了与使用 getDriver().getWindowHandles() 在 windows 之间切换并遍历它相关的答案。

But I would like to know if there is any way to switch between windows using page title in selenium webdriver using java.但我想知道是否有任何方法可以使用 java 在 selenium webdriver 中使用页面标题在 windows 之间切换。

Note: I am a newbie to Selenium Framework注意:我是 Selenium 框架的新手

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:如果您有要将焦点切换到的窗口的确切标题(即窗口的顶级<title>标记的确切内容),那么您只需执行以下操作:

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:如果你使用 xpath 这样的表达式:

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

Where the click() method opens a window. You can switch to that new window with the title of the page. click()方法打开 window 的位置。您可以使用页面标题切换到新的 window。 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:在这种情况下,您可以使用以下方式切换到此 window:

switchTo().window("Amazing");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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