简体   繁体   English

Java Selenium 如何“将图像另存为”并按 Enter

[英]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. 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 然后按回车键保存图像。

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.当我执行此操作时,除了 PRESS ENTER 外,它可以正常工作,测试打开另存为 window 但仍保留在那里,无一例外,它只是无法按 ENTER 键。

Any help?有什么帮助吗?

Keys.ARROW_DOWN within Context Menu上下文菜单中的 Keys.ARROW_DOWN

Context Menu initiated through context_click() is generally invoked on a WebElement eg a link .通过context_click()启动的上下文菜单通常在WebElement上调用,例如链接

@barancev [Member of Selenium] in his comment clearly mentions: @barancev [Selenium 的成员] 在他的评论中明确提到:

contextClick on a link opens a native context menu that can't be managed by Selenium (by design).上下文单击链接会打开无法由 Selenium 管理的本机上下文菜单(按设计)。


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.使用Selenium您将无法使用send_keys(Keys.ARROW_DOWN)send_keys(Keys.DOWN)等与浏览器原生上下文菜单项进行交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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