【问题标题】:Unable to handle newly opened GrandChild window (Lookup) from a child window无法从子窗口处理新打开的 GrandChild 窗口(查找)
【发布时间】:2015-02-12 17:29:31
【问题描述】:

在我的项目中,我尝试自动创建新员工,为此我需要单击一个链接,该链接将从主窗口打开一个子窗口,在该子窗口中我需要单击一个查找按钮,以选择一个报告该员工的经理。单击该查找后,我得到一个新的孙窗口,其中包含经理姓名列表,以便我可以从中选择一个。

以下是我用来在窗口之间移动的代码,

// 从父级移动到子级

    String parentWindow = driver.getWindowHandle();

    Set<String> windowHandles = driver.getWindowHandles();      

    System.out.println(windowHandles.size());

    windowHandles.remove(parentWindow);

   driver.switchTo().window((String) windowHandles.toArray()[0]);

   // Click lookup to emulate Grandchild window
   driver.findElement(..)

   //From child to grandchild

   Set<String> grandChild_Child_ParentHandles=driver.getWindowHandles();

    grandChild_Child_ParentHandles.remove(parentWindow);
    grandChild_Child_ParentHandles.remove(windowHandles);

    System.out.println(grandChild_Child_ParentHandles.size());

    driver.switchTo().window((String) grandChild_Child_ParentHandles.toArray()[0]);

    System.out.println(driver.getTitle());

我的代码一直在运行,直到单击该查找,之后我的代码停止执行。当且仅当我手动关闭 Grandchild 窗口时,代码才会重新开始。我想知道为什么会这样。

请帮忙!

提前致谢,西瓦

【问题讨论】:

  • 我想知道您所说的孙窗口实际上是一个新窗口还是一个javascript警报,基于您的代码在孙窗口打开时停止执行并且代码恢复的语句当它关闭时。尝试一件事。将代码System.out.println("Total windows: "+driver.getWindowHandles().size()); 放在打开孙子的代码之后。让我知道它返回什么......
  • 您好,感谢您的快速回复。我尝试在孙子窗口打开后放置你的代码,正如我之前提到的,只有在我关闭孙子窗口后才会显示窗口总数。而且我已经尝试放置以下警报句柄代码,这也不起作用。警报警报 = driver.switchTo().alert(); System.out.println(aler.getText());并且在关闭孙窗口后,我将“null”作为警报文本。
  • 您能否添加一个屏幕截图作为孙窗口的外观?您可以上传image in imgur并在此处添加链接以供参考。
  • 嗨,请找到下面的共享链接drive.google.com/file/d/0B4jUpzMjM4aNaWlJZ3JCblpNb1E/… P.S:imgur 在我的办公室被阻止。
  • 哦!恐怕这可能是modal window.. 正如维基中所写:a modal window is a graphical control element subordinate to an application's main window which creates a mode where the main window can't be used.. 而且,因此 selenium 不能直接自动化它。您可以借助Robot classSikuli 之类的UI 工具与之交互并继续进行其余操作。

标签: java selenium-webdriver window-handles


【解决方案1】:

我遇到这个问题是因为模态窗口,这可以通过使用多线程概念来解决。通常一旦模态窗口被唤起,它就会阻塞它的父窗口,这意味着我们不能将我们的驱动程序移动到新的子窗口。所以在模态窗口被唤起之后,我们的代码就停止工作了。

尝试参考下面的多线程示例代码,这是我在网络中找到的。

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

class NewThread implements Runnable {
static WebDriver driver = new FirefoxDriver();
Thread t;

NewThread() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
// Start the thread
t.start();
}

// This is the entry point for the second thread.
public void run() {
try {

System.out.println("This thread has got the control now");
Thread.sleep(5000);

} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}

for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}

String nextHandle = driver.getWindowHandle();
System.out.println("nextHnadle" + nextHandle);

try {
Thread.sleep(4000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

driver.findElement(By.id("foo")).clear();
driver.findElement(By.id("foo")).sendKeys("4");
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

driver.findElement(By.linkText("link")).click();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

driver.quit();
System.out.println("Exiting child thread.");
}

// execution will start from here
public static void main(String args[]) throws InterruptedException {
try {
// open the webpage
driver.get("https://developer.mozilla.org/samples/domref/showModalDialog.html");

// get window handle
String currenthandle = driver.getWindowHandle();
System.out.println("currenthandle" + currenthandle);

//start a new thread
new NewThread();

// click on the button to open model window dialog
driver.findElement(By.tagName("input")).click();

} catch (Exception e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}

} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多