无法处理来自 child window 的新打开的 GrandChild window(查找)

Unable to handle newly opened GrandChild window (Lookup) from a child window

在我的项目中,我尝试自动创建一个新员工,为此我需要单击一个 link,它将从主 window 打开一个 child window在那个 child window 中,我需要单击一个查找按钮,select 该员工的报告经理。单击该查找后,我得到一个新的 grandchild window,其中包含经理姓名列表,因此我可以从中 select 一个。

以下是我用来在 windows、

之间移动的代码

// 从 Parent 移动到 Child

    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 window 时,代码才会再次启动。我想知道为什么会这样。

求助!

提前致谢,Siva

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

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

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

}