How can I tell Selenium to press cancel on a print popup?

J. Doe

I am checking whether or not a page appears using Selenium. When I click the page, however, a printer print prompt appears (like the window that says select printer and such). How can I have Selenium close this window by hitting cancel?

I tried looking to alerts, but it seems like those will not work since the print window is a system prompt. It does not recognize any alerts appearing.

The most recent I tried using is by just sending keys like tab and enter in order to have the cancel button selected, however, it doesn't recognize any keys as being pressed.

How can I handle this case?

public static boolean printButton() throws Exception {

    WebDriver driver = new FirefoxDriver();
    driver.get("website");


    try {

        Thread.sleep(3000);
        WebElement temp = driver.findElement(By.xpath("//*[@id='block-print-ui-print-links']/div/span/a"));
        temp.click();
        Actions action = new Actions(driver); 
        action.sendKeys(Keys.TAB).sendKeys(Keys.ENTER);

        Thread.sleep(6000);

     }

     catch (Exception e) {

        System.out.println("No button.");
        driver.close();
        return false;

     }  
Florent B.

I would simply disable the print dialog by overriding the print method :

((JavascriptExecutor)driver).executeScript("window.print=function(){};");

But if you goal is to test that the printing is called then :

// get the print button
WebElement print_button = driver.findElement(By.cssSelector("..."));

// click on the print button and wait for print to be called
driver.manage().timeouts().setScriptTimeout(20, TimeUnit.SECONDS);
((JavascriptExecutor)driver).executeAsyncScript(
    "var callback = arguments[1];" +
    "window.print = function(){callback();};" +
    "arguments[0].click();"
    , print_button);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How can I tell Selenium to press cancel on a print popup?

分類Dev

How do I print this pattern?Can anyone tell me the conditions(if statement)?

分類Dev

How can I cancel Collections.sort()

分類Dev

Selenium Python I can iterate over the html table how do I print the values from column 3

分類Dev

How can I tell a function enters kernel or not

分類Dev

Magento - How can I tell if I'm on a "My Account" page?

分類Dev

How can I tell which user limit I am running into?

分類Dev

How can I tell if I am out of inotify watches?

分類Dev

How can I tell how many bits my ssh key is?

分類Dev

How can I capture a table row press action in Swift WatchKit?

分類Dev

How can I programmatically tell if a filename matches a shell glob pattern?

分類Dev

XMLEventWriter: how can I tell it to write empty elements?

分類Dev

How can I tell which mysql user edited a table in mysql

分類Dev

Ember: How can I tell if peekAll returns any models?

分類Dev

How can I tell if my current PowerShell session is over SSH?

分類Dev

How can I tell meson in which directories to look for dependencies?

分類Dev

How can I tell if my vector matches the variable values in r?

分類Dev

How can I tell through the Stripe API if a connected account is complete?

分類Dev

How can I tell django 1.7 to put migration into specific folder

分類Dev

How can I tell which web server Zimbra 8.0.6 is running?

分類Dev

How can I tell whether a build is Debian-based?

分類Dev

How can I tell Brunch not to concatenate javascript files?

分類Dev

How can I tell which IDE an Android project was made with?

分類Dev

How can I tell Resharper to format these field attributes differently?

分類Dev

How can I print multidimensional arrays in C?

分類Dev

How can I print "-n" with `echo`?

分類Dev

how can i print this using String Methods

分類Dev

How can I open a popup window to alarm user and running in background?

分類Dev

In F#, how do I tell if an object is an Async<_>, and how can I cast it to an Async<_>?

Related 関連記事

  1. 1

    How can I tell Selenium to press cancel on a print popup?

  2. 2

    How do I print this pattern?Can anyone tell me the conditions(if statement)?

  3. 3

    How can I cancel Collections.sort()

  4. 4

    Selenium Python I can iterate over the html table how do I print the values from column 3

  5. 5

    How can I tell a function enters kernel or not

  6. 6

    Magento - How can I tell if I'm on a "My Account" page?

  7. 7

    How can I tell which user limit I am running into?

  8. 8

    How can I tell if I am out of inotify watches?

  9. 9

    How can I tell how many bits my ssh key is?

  10. 10

    How can I capture a table row press action in Swift WatchKit?

  11. 11

    How can I programmatically tell if a filename matches a shell glob pattern?

  12. 12

    XMLEventWriter: how can I tell it to write empty elements?

  13. 13

    How can I tell which mysql user edited a table in mysql

  14. 14

    Ember: How can I tell if peekAll returns any models?

  15. 15

    How can I tell if my current PowerShell session is over SSH?

  16. 16

    How can I tell meson in which directories to look for dependencies?

  17. 17

    How can I tell if my vector matches the variable values in r?

  18. 18

    How can I tell through the Stripe API if a connected account is complete?

  19. 19

    How can I tell django 1.7 to put migration into specific folder

  20. 20

    How can I tell which web server Zimbra 8.0.6 is running?

  21. 21

    How can I tell whether a build is Debian-based?

  22. 22

    How can I tell Brunch not to concatenate javascript files?

  23. 23

    How can I tell which IDE an Android project was made with?

  24. 24

    How can I tell Resharper to format these field attributes differently?

  25. 25

    How can I print multidimensional arrays in C?

  26. 26

    How can I print "-n" with `echo`?

  27. 27

    how can i print this using String Methods

  28. 28

    How can I open a popup window to alarm user and running in background?

  29. 29

    In F#, how do I tell if an object is an Async<_>, and how can I cast it to an Async<_>?

ホットタグ

アーカイブ