简体   繁体   中英

Java Selenium How to “save image as” and press Enter

I'm trying to achieve the following: I have the following url: https://img.pokemondb.net/sprites/sword-shield/icon/bulbasaur.png I need to press ctrl + s via Selenium to open the download Windows window and then press enter in order to save the image.

This is my code:

 WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();

    Robot robot = new Robot();

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    
    String url = "https://img.pokemondb.net/sprites/sword-shield/icon/bulbasaur.png";
    driver.get(url);

    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_S);
    robot.delay(1000);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

When I execute this, it works everything EXCEPT the PRESS ENTER, the test open the save as window but remain there, without any exception, it just cannot press ENTER key.

Any help?

Keys.ARROW_DOWN within Context Menu

Context Menu initiated through context_click() is generally invoked on a WebElement eg a link .

@barancev [Member of Selenium] in his comment clearly mentions:

contextClick on a link opens a native context menu that can't be managed by Selenium (by design).


Conclusion

Using Selenium you won't be able to interact with browser native context menu items using send_keys(Keys.ARROW_DOWN) , send_keys(Keys.DOWN) , etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM