Selenium Processing modal dialog box
Problem Description:
Click the button to appear a modal dialog box, the code will be stuck in Click this step does not continue to execute. The reason for this is that selenium currently does not provide the processing of the modal dialog box.
Solution:
Click on the pop-up box this step with JS instead of execution, and then switch to the pop-up window can continue to manipulate the page elements.
Test Address: https://developer.mozilla.org/samples/domref/showModalDialog.html
The code is as follows:
Public class Junittest {
Webdriver Driver = new firefoxdriver ();
String baseUrl = "https://developer.mozilla.org/samples/domref/showModalDialog.html";
@Test
Public void Openmodal () throws interruptedexception{
Driver.get (BASEURL);
Driver.findelement (by. XPath("/html/body/input")). Click (); Click the Post code card here
Thread. Sleep (2000);
set<string> handlers = Driver.getwindowhandles ();
for (String winhandler:handlers) {
Driver.switchto (). window (Winhandler);
}
Driver.findelement (by. ID("foo")). SendKeys ("2");
Driver.findelement (by. XPath("/html/body/input[2]"). Click ();
}
}
Click this step to replace the JS execution code:
Public class Junittest {
Webdriver Driver = new firefoxdriver ();
String baseUrl = "https://developer.mozilla.org/samples/domref/showModalDialog.html";
@Test
Public void Openmodal () throws interruptedexception{
Driver.get (BASEURL);
Driver.findelement (By.xpath ("/HTML/body/input")). Click (); Click the Post code card here
String js = "setTimeout (function () {document.getelementsbytagname (' input ') [0].click ()},100)";
((Javascriptexecutor) driver). Executescript (JS);
Thread. Sleep (2000);
set<string> handlers = Driver.getwindowhandles ();
for (String winhandler:handlers) {
Driver.switchto (). window (Winhandler);
}
Driver.findelement (by. ID("foo")). SendKeys ("2");
Driver.findelement (by. XPath("/html/body/input[2]"). Click ();
}
}
Selenium Processing modal dialog box