-1

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

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

// 从父级移动到子级

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

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

请帮忙!

提前致谢, 湿婆

4

1 回答 1

0

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

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

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

} 
于 2015-02-13T15:30:23.867 回答