Use visual analysis to dismiss the web browser pop-up window

Automating web apps includes dealing with various web browser features. This includes the browser prompting the user to agree to certain actions, for example to remember a password, or provide location information.

The problem, for the automation script, with these pop-up windows is that they are not part of the web app page and therefore not accessible using the standard Selenium tools. Perfecto's visual analysis tools, tried and tested in the mobile devices arena, provide the extra support to deal directly with these popup areas.

Use OCR to dismiss a pop-up window

The following code snippet shows how to use the Perfecto text:select command to identify and click the Share Location button on the popup shown above.

Copy
driver.get("https://www.yahoo.com/news/weather/");
driver.findElementByXPath("//*[text()='Change location']").click();
driver.findElementByXPath("//*[text()='Detect my location']").click();
            
try {
    Map<String, Object> params = new HashMap<>();
    params.put("content", "Share location");
    params.put("index", "1");
    params.put("timeout", 10);
    params.put("screen.top", "0");
    params.put("screen.height", "30%");
    params.put("screen.width", "35%");
    driver.executeScript("mobile:text:select", params);
} catch (Exception e) {
    System.out.println("No browser popup found, continue with script");
}

Some short notes about the code:

  • We use the screen.top, screen.height, and screen.width parameters to limit the area of the screen searched. This makes the visual analysis operation quicker.
  • Use the index parameter if the string that you are searching for appears multiple times on the screen and you need to identify (for example) the second or third appearance.
  • The command throws an exception if the text is not found.
  • The timeout parameter can be used to wait for the text to appear on the screen. Similar to wait functions in Selenium.

The full sample can be seen in the Perfecto CommunitySamples GitHub: https://github.com/PerfectoCode/Community-Samples/tree/master/DismissWebPopup