Skip to content Skip to sidebar Skip to footer

How To Click On The Print Button On A Web Page Using Selenium

I want to click on the print button in this page : https://www.google.com/maps/dir/40.4515849,-3.6903752/41.380896,2.1228198/@40.4515849,-3.6903752/am=t/?hl=en and then save the P

Solution 1:

Here's my insight.

First of all, you need to wait for the page to load in order to interact with the Print button. The best way to go is to use built-in mechanism: selenium waits - wait for the Print button to be clickable:

// open print dropdown
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.print-button"))).click();

// click print button
WebElement printButton = driver.findElement(By.cssSelector("button.print-popup-button"));
printButton.click();

Okay, if you run it using ChromeDriver:

package com.company;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class Main {
    public static void main(String[] args) {
        String url = "https://www.google.com/maps/dir/40.4515849,-3.6903752/41.380896,2.1228198/@40.4515849,-3.6903752/am=t/?hl=en";

        WebDriver driver = new ChromeDriver();
        driver.get(url);

        // open print dropdown
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.print-button"))).click();

        // click print button
        WebElement printButton = driver.findElement(By.cssSelector("button.print-popup-button"));
        printButton.click();

        // now what? 
    }
}

You'll see the Chrome Print Preview dialog, which is, unfortunately, out of scope for selenium:

enter image description here

But, there is a hope, if you examine available Chrome arguments, you would see that there is the relevant one:

--disable-print-preview - Disables print preview (For testing, and for users who don't like us. :[ )

Okay, let's try it out:

ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-print-preview");

WebDriver driver = new ChromeDriver(options);
driver.get(url);

Now, there is a system print dialog being shown:

enter image description here

Selenium cannot control it too. So, nope, there is no hope. Oh, wait!


Okay, if we are out of scope of selenium, let's use tools that can help us to click that Print button in the dialog - Robot class:

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed.

We'll initialize the Robot and will send Enter key when the print dialog would show up:

package com.company;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.awt.*;
import java.awt.event.KeyEvent;


public class Main {
    public static void main(String[] args) throws AWTException, InterruptedException {
        String url = "https://www.google.com/maps/dir/40.4515849,-3.6903752/41.380896,2.1228198/@40.4515849,-3.6903752/am=t/?hl=en";

        Robot r = new Robot();
        r.delay(1000);

        WebDriver driver = new ChromeDriver();
        driver.get(url);

        // open print dropdown
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.print-button"))).click();

        // click print button
        WebElement printButton = driver.findElement(By.cssSelector("button.print-popup-button"));
        printButton.click();

        Thread.sleep(2000);

        r.keyPress(KeyEvent.VK_ENTER);
        r.keyRelease(KeyEvent.VK_ENTER);
    }
}

Other options are to use:

  • sikuli - you would need an image of the print button in order for sikuli to locate it and click
  • autoit

Also see:


Solution 2:

This way you can click the Print including maps-option (sorry, but it is in C#):

Actions builder = new Actions(Driver);
builder.MoveToElement(Driver.FindElement(By.ClassName("print-button"))).Click()
       .MoveToElement(Driver.FindElements(By.ClassName("print-popup-button"))[0]).Click().Build().Perform();            

But! In your question you had a picture of Chrome print preview window, which I think won't be available with HtmlUnitDriver, which your code is using. You can always run the test with chromedriver, but it seems like the print preview is out of scope for selenium, ie. you can't control it with webdriver. To my knowledge the same is true for system print dialog, which appears with firefox. If HtmlUnitDriver behaves differently in this case, then you'll be fine, but I'm afraid you are going to face a problem here.


Solution 3:

Just to add to what @JoriO posted, Can you please test to see if the web element exists before clicking on it using Selenium? If it doesn't exists, add a thread sleep statement after driver.get(url)... Reason being that Selenium may try to click before the driver finishes loading the page. This has fixed most of the problems I have experienced using Selenium error "No Such Element Exception"


Post a Comment for "How To Click On The Print Button On A Web Page Using Selenium"