久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 Selenium 在網(wǎng)頁上單擊打印按鈕

How to click on the print button on a web page using Selenium(如何使用 Selenium 在網(wǎng)頁上單擊打印按鈕)
本文介紹了如何使用 Selenium 在網(wǎng)頁上單擊打印按鈕的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想點(diǎn)擊這個(gè)頁面的打印按鈕:

,還是有希望的,你會(huì)看到有相關(guān)的:

<塊引用>

--disable-print-preview - 禁用打印預(yù)覽(用于測試和不喜歡我們的用戶.:[)

好的,我們試試看:

ChromeOptions 選項(xiàng) = new ChromeOptions();options.addArguments("--disable-print-preview");WebDriver driver = new ChromeDriver(options);driver.get(url);

現(xiàn)在,顯示了一個(gè)系統(tǒng)打印對話框:

Selenium 也無法控制它.所以,不,沒有希望.哦,等等!

<小時(shí)>

好的,如果我們超出了 selenium 的范圍,讓我們使用可以幫助我們單擊對話框中的 Print 按鈕的工具 - Robot 類:

<塊引用>

該類用于為測試自動(dòng)化、自運(yùn)行演示和其他目的需要控制鼠標(biāo)和鍵盤的應(yīng)用程序.

我們將初始化 Robot 并在打印對話框出現(xiàn)時(shí)發(fā)送 Enter 鍵:

包c(diǎn)om.company;導(dǎo)入 org.openqa.selenium.By;導(dǎo)入 org.openqa.selenium.WebDriver;導(dǎo)入 org.openqa.selenium.WebElement;導(dǎo)入 org.openqa.selenium.chrome.ChromeDriver;導(dǎo)入 org.openqa.selenium.support.ui.ExpectedConditions;導(dǎo)入 org.openqa.selenium.support.ui.WebDriverWait;導(dǎo)入 java.awt.*;導(dǎo)入 java.awt.event.KeyEvent;公共類主要{public static void main(String[] args) 拋出 AWTException,InterruptedException {字符串 url = "https://www.google.com/maps/dir/40.4515849,-3.6903752/41.380896,2.1228198/@40.4515849,-3.6903752/am=t/?hl=en";機(jī)器人 r = new Robot();r.延遲(1000);WebDriver driver = new ChromeDriver();driver.get(url);//打開打印下拉菜單WebDriverWait 等待 = new WebDriverWait(驅(qū)動(dòng)程序, 10);wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.print-button"))).click();//點(diǎn)擊打印按鈕WebElement printButton = driver.findElement(By.cssSelector("button.print-popup-button"));printButton.click();線程.sleep(2000);r.keyPress(KeyEvent.VK_ENTER);r.keyRelease(KeyEvent.VK_ENTER);}}

使用其他選項(xiàng):

  • sikuli - 您需要打印按鈕的圖像才能sikuli 找到它并點(diǎn)擊
  • autoit

另見:

  • 如何在 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 PDF...

this is the code for click the button:

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("http://*[@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:

解決方案

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:

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:

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:

  • How to handle print dialog in Selenium?

這篇關(guān)于如何使用 Selenium 在網(wǎng)頁上單擊打印按鈕的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯(cuò)誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 欧产日产国产精品视频 | 欧美在线一区二区三区 | 天天色天天射天天干 | 日韩免费1区二区电影 | 国产精品久久久亚洲 | 国产一区不卡 | 99热国产免费| 我爱操 | 网页av| 亚洲精品乱码久久久久久按摩观 | 久久99精品久久久久久 | 日韩三级视频 | 亚洲播放 | 亚洲一区二区三区在线免费 | 国产精品a免费一区久久电影 | 国产韩国精品一区二区三区 | 国产在线观看一区二区三区 | 亚洲日本一区二区三区四区 | 日韩精品一二三区 | 亚洲精品国产一区 | 欧美日韩专区 | 国产一区不卡 | 国产精品久久久久久久模特 | 久久精品一 | 日韩精品免费在线观看 | 亚洲免费在线 | 国产露脸对白88av | 亚洲精品视频一区 | 久久成人免费观看 | av色站| 国产日本精品视频 | 国产精品美女 | 国产成人午夜高潮毛片 | 精品在线观看一区二区 | 免费av观看 | 一级毛片在线播放 | 激情91| 亚洲欧洲一区二区 | 在线视频中文字幕 | 中文字幕一区在线观看视频 | 久久精品福利视频 |