Here is my understanding.
First of all, you need to wait for the page to load in order to interact with the Print button. It is best to use the built-in mechanism: selenium is waiting - wait for the Print button to be pressed:
// 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();
Well, 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);
You will see the Chrome preview dialog, which unfortunately goes beyond selenium:

But there is hope, if you look at the available arguments of Chrome , you will see that there is a corresponding one:
- disable-print-preview - disables print preview (for testing and for users we donβt like.: [)
Ok try:
ChromeOptions options = new ChromeOptions(); options.addArguments("--disable-print-preview"); WebDriver driver = new ChromeDriver(options); driver.get(url);
The system print dialog box now appears:

Selenium also cannot control this. So no, there is no hope. Oh wait!
Well, if we do not enter the realm of selenium, let us use tools that can help us click this Print button in the dialog box - Robot class:
This class is used to generate your own system input events for the purpose of testing automation, stand-alone demonstrations and other applications that require mouse and keyboard control.
We initialize Robot and send Enter when the print dialog appears:
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);
Other options:
sikuli - you will need an image of the print button so that the siculi find it and clickautoit
See also: