pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何使用 Selenium 在網頁上單擊打印按鈕

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

問題描述

我想點擊這個頁面的打印按鈕:

,還是有希望的,你會看到有相關的:

<塊引用>

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

好的,我們試試看:

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

現在,顯示了一個系統打印對話框:

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

<小時>

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

<塊引用>

該類用于為測試自動化、自運行演示和其他目的需要控制鼠標和鍵盤的應用程序.

我們將初始化 Robot 并在打印對話框出現時發送 Enter 鍵:

包com.company;導入 org.openqa.selenium.By;導入 org.openqa.selenium.WebDriver;導入 org.openqa.selenium.WebElement;導入 org.openqa.selenium.chrome.ChromeDriver;導入 org.openqa.selenium.support.ui.ExpectedConditions;導入 org.openqa.selenium.support.ui.WebDriverWait;導入 java.awt.*;導入 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";機器人 r = new Robot();r.延遲(1000);WebDriver driver = new ChromeDriver();driver.get(url);//打開打印下拉菜單WebDriverWait 等待 = new WebDriverWait(驅動程序, 10);wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.print-button"))).click();//點擊打印按鈕WebElement printButton = driver.findElement(By.cssSelector("button.print-popup-button"));printButton.click();線程.sleep(2000);r.keyPress(KeyEvent.VK_ENTER);r.keyRelease(KeyEvent.VK_ENTER);}}

使用其他選項:

  • sikuli - 您需要打印按鈕的圖像才能sikuli 找到它并點擊
  • 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?

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

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

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 河南正规膏药生产厂家-膏药贴牌-膏药代加工-修康药业集团官网 | 跨境物流_美国卡派_中大件运输_尾程派送_海外仓一件代发 - 广州环至美供应链平台 | 湖州织里童装_女童男童中大童装_款式多尺码全_织里儿童网【官网】-嘉兴嘉乐网络科技有限公司 | 河南档案架,档案密集架,手动密集架,河南密集架批发/报价 | 新型锤式破碎机_新型圆锥式_新型颚式破碎机_反击式打沙机_锤式制砂机_青州建源机械 | 土壤墒情监测站_土壤墒情监测仪_土壤墒情监测系统_管式土壤墒情站-山东风途物联网 | 槽钢冲孔机,槽钢三面冲,带钢冲孔机-山东兴田阳光智能装备股份有限公司 | 贴片电感_贴片功率电感_贴片绕线电感_深圳市百斯特电子有限公司 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 蔬菜清洗机_环速洗菜机_异物去除清洗机_蔬菜清洗机_商用洗菜机 - 环速科技有限公司 | 尚为传动-专业高精密蜗轮蜗杆,双导程蜗轮蜗杆,蜗轮蜗杆减速机,蜗杆减速机生产厂家 | 滑石粉,滑石粉厂家,超细滑石粉-莱州圣凯滑石有限公司 | Maneurop/美优乐压缩机,活塞压缩机,型号规格,技术参数,尺寸图片,价格经销商 | 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 冷却塔减速机器_冷却塔皮带箱维修厂家_凉水塔风机电机更换-广东康明冷却塔厂家 | WF2户外三防照明配电箱-BXD8050防爆防腐配电箱-浙江沃川防爆电气有限公司 | 定量包装机,颗粒定量包装机,粉剂定量包装机,背封颗粒包装机,定量灌装机-上海铸衡电子科技有限公司 | 起好名字_取个好名字_好名网免费取好名在线打分 | 医疗仪器模块 健康一体机 多参数监护仪 智慧医疗仪器方案定制 血氧监护 心电监护 -朗锐慧康 | 气动机械手-搬运机械手-气动助力机械手-山东精瑞自动化设备有限公司 | 杭州翻译公司_驾照翻译_专业人工翻译-杭州以琳翻译有限公司官网 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 | 临朐空调移机_空调维修「空调回收」临朐二手空调 | 中天寰创-内蒙古钢结构厂家|门式刚架|钢结构桁架|钢结构框架|包头钢结构煤棚 | 影合社-影视人的内容合作平台 | 宏源科技-房地产售楼系统|线上开盘系统|售楼管理系统|线上开盘软件 | 轴流风机-鼓风机-离心风机-散热风扇-罩极电机,生产厂家-首肯电子 | 酒店品牌设计-酒店vi设计-酒店标识设计【国际级】VI策划公司 | 球盟会·(中国)官方网站 | HEYL硬度计量泵-荧光法在线溶解氧仪-净时测控技术(上海)有限公司 | 沈阳庭院景观设计_私家花园_别墅庭院设计_阳台楼顶花园设计施工公司-【沈阳现代时园艺景观工程有限公司】 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | 小威小说网 - 新小威小说网 - 小威小说网小说搜索引擎 | 包装设计公司,产品包装设计|包装制作,包装盒定制厂家-汇包装【官方网站】 | SDI车窗夹力测试仪-KEMKRAFT方向盘测试仪-上海爱泽工业设备有限公司 | 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 中药二氧化硫测定仪,食品二氧化硫测定仪|俊腾百科 | 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 铣刨料沥青破碎机-沥青再生料设备-RAP热再生混合料破碎筛分设备 -江苏锡宝重工 | 涿州网站建设_网站设计_网站制作_做网站_固安良言多米网络公司 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | IIS7站长之家-站长工具-爱网站请使用IIS7站长综合查询工具,中国站长【WWW.IIS7.COM】 |