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

35g0bw71  于 5个月前  发布在  Eclipse
关注(0)|答案(5)|浏览(49)

我找到了与使用getDriver().getWindowHandles()在窗口之间切换和迭代有关的答案。
但我想知道是否有任何方法可以在使用java的selenium webdriver中使用页面标题在窗口之间切换。

rsl1atfo

rsl1atfo1#

你可以的。

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);
   } 
}

字符串

hi3rlvi2

hi3rlvi22#

如果你有你想切换焦点的窗口的确切标题(即窗口的顶层<title>标签的确切内容),那么你可以简单地做以下事情:

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

字符串
如果你只有你想切换焦点的窗口的部分标题,那么你应该像其他答案建议的那样,用它们的句柄覆盖可用的窗口。

vawmfj5a

vawmfj5a3#

引用:代码切换回父窗口的情况下,标题的窗口没有找到。

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

字符串
引用:代码获取原始窗口句柄的情况下,标题是找不到的窗口。

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

将尝试根据窗口标题进行切换的代码。

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
        }
    }

h7wcgrx3

h7wcgrx34#

你可以试试下面的

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);
}
}

字符串
希望这有助于你正在寻找的东西。

kdfy810k

kdfy810k5#

如果你使用的是这样的xpath表达式:

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

字符串
click()方法打开一个窗口。您可以使用页面标题切换到新窗口。标题在标题中显示,如下所示:

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

<h1>Heading</h1>

</body>
</html>


在这种情况下,您可以使用以下命令切换到此窗口:

switchTo().window("Amazing");

相关问题