在我的项目中,我尝试自动创建新员工,因为我需要单击一个链接,该链接将从主窗口打开子窗口,在该子窗口中我需要单击查找按钮,以选择报告该员工的经理。点击该查找后,我得到一个新的孙子窗口,其中包含经理名称列表,以便我可以从中选择一个。
以下是我用来在窗口之间移动的代码,
//从父母移到儿童
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());
我的代码一直运行直到单击该查找,之后我的代码停止执行。当代码只有我手动关闭孙子窗口时代码再次启动。我想知道它为什么会发生。
请帮忙!
先谢谢,Siva
答案 0 :(得分:0)
由于Modal窗口我遇到了这个问题,可以通过使用多线程概念来解决。通常一旦模态窗口被激发,它将阻止其父窗口,这意味着我们无法将驱动程序移动到新的子窗口。因此,在模态窗口唤起之后,我们的代码停止工作。
尝试引用以下示例代码进行多线程处理,我在网上找到了。
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.");
}
}