2

My scenario is:

  1. main window-> do some activity.
  2. click on Save button-> Confirmation pop up open with OK and CANCEL button.
  3. Click on OK button on confirmation popup ->another success pop up open with OK button.
  4. click on OK button on success pop up.
  5. switch to main window.

Above PopUp is HTML pop ups. How do i handle above scenario in selenium?. I am new on selenium. Please help me.I am stuck on above point.

enter image description here

Code

String ParentWindow = driver.getWindowHandle(); //switching from parent to pop up window 
for (String Child_Window : driver.getWindowHandles()) { 
driver.switchTo().window(Child_Window);
WebDriverWait wait = new WebDriverWait(driver, 30);// 1 minute 
wait.until(ExpectedConditions.visibilityOfElementLocated(By.‌​name("test")));   
driver.findElement(By.xpath("//input[@value='test']")).click‌​();
} 
driver.switchTo().window(ParentWindow); 

HTML

<div>
<div class="msgBoxContainer">
<div id="msgBox1473308035532Image" class="msgBoxImage">
<img src="styles/images/confirm.png">
</div>
<div id="msgBox1473308035532Content" class="msgBoxContent">
<p>
<span>Saveでよろしいですか??</span>
</p>
</div>
</div>
<div id="msgBox1473308035532Buttons" class="msgBoxButtons">
<input id="msgBox1473308035532FirstButton" class="msgButton" type="button"  value="はい" name="はい">
<input class="msgButton" type="button" value="いいえ" name="いいえ">
</div>
</div>
</div>

// When click on OK button of first popup the respective div is destroy and new div getting generated for Second pop up

<div id="msgBox1473308225709" class="msgBox" style="background-image:     url("styles/images/msgBoxBackGround.png"); opacity: 1; top: 52.5px; left: 566.5px;">
<div class="msgBoxTitle">Information</div>
<div>
<div class="msgBoxContainer">
<div id="msgBox1473308225709Image" class="msgBoxImage">
<img src="styles/images/info.png">
</div>
<div id="msgBox1473308225709Content" class="msgBoxContent">
<p>
<span>登録完了</span>
</p>
</div>
</div>
<div id="msgBox1473308225709Buttons" class="msgBoxButtons">
<input id="msgBox1473308225709FirstButton" class="msgButton" type="button"   value="はい" name="はい">
</div>
</div>
</div>
6
  • i have written below code to switch from main window to first pop up, but unable to switch from first pop up to second. String ParentWindow = driver.getWindowHandle(); //switching from parent to pop up window for (String Child_Window : driver.getWindowHandles()) { driver.switchTo().window(Child_Window); WebDriverWait wait = new WebDriverWait(driver, 30);// 1 minute wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("test"))); driver.findElement(By.xpath("//input[@value='test']")).click(); } driver.switchTo().window(ParentWindow); Commented Sep 7, 2016 at 2:32
  • Could you share these popup screenshot?? Commented Sep 7, 2016 at 2:54
  • As I'm seeing your popup screen shot this is a simple HTML dialog box, could you share both dialog HTML as well for better solution, and FYI no need to switch window here because it's not a window popup.. Commented Sep 7, 2016 at 19:09
  • @ Saurabh. Thanks. Now i have added HTML block. Please have a look and suggest me. I am stuck on this point Commented Sep 8, 2016 at 4:28
  • 1
    Finally resolved the issue. there was an one div which appears on pop up.that's why system unable to click on the element. Commented Sep 8, 2016 at 8:45

3 Answers 3

2

After click on Save button you should handle these information dialog box as below :-

WebDriverWait wait = new WebDriverWait(driver,10);

//For first dialog box
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']"))).click();

//Now same as for second dialog box
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']"))).click();

Note:- There is no need to switch window, these dialog box are simple HTML elements, So you need handle it normally by finding these elements.

Edited1 :- If you're unable to click using WebElement.click() try using Actions class to move that element before click as below :-

Actions act = new Actions(driver);

//For first dialog box
WebElement firstDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
act.moveToElement(firstDialog).click().perform();

//Now same as for second dialog box
WebElement secondDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
act.moveToElement(secondDialog).click().perform();

Edited2:- If you're still unable to click try using JavascriptExecutor as below :-

//For first dialog box
WebElement firstDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",firstDialog);

//Now same as for second dialog box
WebElement secondDialog = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.msgButton[id*='msgBox'][id*='FirstButton']")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()",secondDialog);
Sign up to request clarification or add additional context in comments.

6 Comments

@saurabh..Thank you so much.. Tried above solution and error occurred as: org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (1552, 47). Other element would receive the click: <div id="msgBox1473311515350BackGround" class="msgBoxBackGround" style="opacity: 0.1; width: 1600px; height: 799px;"></div>
@Swa try using Actions class then, see updated answer and let me know..
@saurabh..Tried and same error occurred..tried on Firefox, IE and chrome browser but error seems there. Actually below div is present before pop up div. Is it creating issue? I don't know exactly. <div id="msgBox1473311515350BackGround" class="msgBoxBackGround" style="opacity: 0.1; width: 1600px; height: 799px;"></div>
@Swa And when this div getting disappeared??
That div disappears after destroyed the both pop up's div. when first pop up open above div & popup div appears, when we click on OK button of first pop up,popup div destroyed but above div still there and second pop up open. when we click on ok button of second pop both popup and above div getting disappeared.
|
1

1) main window-> do some activity.

Write your code

2) click on Save button-> Confirmation pop up open with OK and CANCEL button.

for confirmation poop up

  Alert simpleAlert = driver.switchTo().alert();
        String alertText = simpleAlert.getText();
        System.out.println("Alert text is " + alertText);
        simpleAlert.accept();

3) Click on OK button on confirmation popup ->another success pop up open with OK button.

another success pop up open with OK button.

simpleAlert.accept();

4) click on OK button on success pop up.

 WebDriverWait wait = new WebDriverWait(driver, 30);
  simpleAlert.accept();

5) Switch to main window.

 driver.switchTo().defaultContent();

Or You can use below code

String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;

Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
    subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler); // switch to popup window
                                            // perform operations on popup

driver.switchTo().window(parentWindowHandler);  

3 Comments

@ shailendra ..thank you for reply..I am really sorry, the pop ups are HTML pop ups.. firepath path pop up button is: html/body/div[38]/div[2]/div[2]/input HTML Code is: <input id="msgBox1473232475674FirstButton" class="msgButton" type="button" value="はい" name="はい"/>
@shailendra..Thanks..used above code (second solution) and below error occurred . I have added wait and visibility code. system click on OK button of first popup and second popup open but unable to perform any action. org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (1552, 47). Other element would receive the click:
@Swa This kind of execption come when element location is not fixed read this seleniumeasy.com/selenium-tutorials/…
0

Since it is java script pop up, you can use alert class.

    WebDriverWait wait = new WebDriverWait(driver, 30);
    // With first Alert
    Alert firstAlert = wait.until(ExpectedConditions.alertIsPresent());
    firstAlert.accept();

    // With Second alert
    Alert secondAlert = wait.until(ExpectedConditions.alertIsPresent());
    secondAlert.accept();

    driver.switchTo().defaultContent();

2 Comments

..Thanks for reply but it's not working and showing error as .. org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (999, 464). Other element would receive the click:
Try finding if there is any iframe or not. This might not be an alert. It might be a modal dialog rapped in a iframe. If you find iFrame, first switch to it and try clicking the element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.