Selenium Processing modal dialog box

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.