How to click the print button on a web page using Selenium

I want to click the print button on this page:

https://www.google.com/maps/dir/40.4515849,-3.6903752/41.380896,2.1228198/@40.4515849,-3.6903752/am=t/?hl=en

enter image description here

and then save the pdf ...

enter image description here

this is the code for the button click:

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 HtmlUnitDriver(); driver.get(url); System.out.println(driver.getTitle()); System.out.println(driver.getCurrentUrl()); WebElement element = driver.findElement(By.xpath("//*[@id=\"text-mode-options-header\"]/div/div/div[2]/div[2]/div/button[1]")); element.click(); System.out.println("Page title is: " + driver.getTitle()); driver.quit(); 

but I get the following error:

  Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using //*[@id="text-mode-options-header"]/div/div/div[2]/div[2]/div/button[1] For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:58' Driver info: driver.version: HtmlUnitDriver at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1057) at org.openqa.selenium.By$ByXPath.findElement(By.java:357) at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1575) at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1) at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1251) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1572) at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:532) at com.controlstation.start.Main.main(Main.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:483) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 

How can I do this using Selenium? Is there another way? thanks.

EDIT:

enter image description here

+6
source share
3 answers

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); // 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 will see the Chrome preview dialog, which unfortunately goes beyond selenium:

enter image description here

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:

enter image description here

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); // 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:

  • sikuli - you will need an image of the print button so that the siculi find it and click
  • autoit

See also:

+4
source

This way you can click 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 got an image of the Chrome preview window, which, I think, will not be available with the HtmlUnitDriver that uses your code. You can always run the test with chromedriver, but it seems that print preview goes beyond selenium, i.e. you cannot control it using webdriver. As far as I know, this is also true for the system print dialog that appears with firefox. If in this case the HtmlUnitDriver behaves differently, then you will be fine, but I'm afraid that you ran into a problem here.

+1
source

Just add @JoriO to the post. Can you check if a web element exists before clicking on it using Selenium? If it does not exist, add a sleep thread operator after driver.get (url) ... The reason is that Selenium may try to click before the driver finishes loading the page. This fixed most of the problems I encountered using the Selenium error "No Such Element Exception"

0
source

Source: https://habr.com/ru/post/977114/


All Articles