-1

私のプロジェクトでは、Creating a new employee を自動化しようとしました。そのためには、メイン ウィンドウから子ウィンドウを開くリンクをクリックする必要があり、その子ウィンドウでルックアップ ボタンをクリックして、そのレポート マネージャーを選択する必要があります。従業員 。そのルックアップをクリックすると、マネージャー名のリストを含む新しい孫ウィンドウが表示されるので、そこからマネージャーを選択できます。

以下は、ウィンドウ間を移動するために使用したコードです。

// 親から子へ移動

    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

モーダルウィンドウが原因でこの問題に直面しましたが、これはマルチスレッドの概念を使用して解決できます。通常、モーダル ウィンドウが呼び出されると、その親ウィンドウがブロックされます。つまり、ドライバーを新しい子ウィンドウに移動することはできません。したがって、モーダル ウィンドウが呼び出された後、コードは動作を停止します。

私がネットで見つけたマルチスレッド用の以下のサンプルコードを参照してみてください。

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 に答える