简体   繁体   English

通过Java中的Selenium Web驱动程序处理另存为对话框

[英]Handle save as dialog box by selenium web driver in java

I'm Trying to save a Html Page using Selenium script in java which perform following operations 我正在尝试使用Java中的Selenium脚本保存HTML页面,该脚本执行以下操作

  1. open the web page. 打开网页。
  2. fire a right click event. 触发右键单击事件。
  3. click on save as html option. 单击另存为html选项。

Now problem is when script click on save as html option it shows a pop-up to save html. 现在的问题是,当脚本单击“另存为html”选项时,它会显示一个弹出窗口以保存html。

I already set default option for download in Firefox but when i run the script it keep showing me pop-up again and again what i need is:- 我已经设置了在Firefox中下载的默认选项,但是当我运行脚本时,它会不断显示我弹出窗口,我需要的是:-

  1. when Firefox shows a save as dialog box then it'll fire an enter event so that it can save html by using Selenium script. 当Firefox显示“另存为”对话框时,它将触发enter事件,以便可以使用Selenium脚本保存html。

Robot class in java might help you. Java中的Robot类可能会对您有所帮助。 Check this link as well. 还要检查此链接

Here is a sample code: 这是一个示例代码:

// Create object of Robot class<br>
Robot object=new Robot();

// Press Enter<br>
object.keyPress(KeyEvent.VK_ENTER);

// Release Enter<br>
object.keyRelease(KeyEvent.VK_ENTER);

Window pop ups cant be handled by Selenium.So you can go for follwing options : - Selenium无法处理窗口弹出窗口。因此,您可以使用以下选项:-

(1) Java Robot Class (1)Java机器人课程

(2) Sikuli [Recommended] (2) Sikuli [推荐]

(3) AutoIt (3)AutoIt

These are 3rd party tools used to handle window based pop ups. 这些是用于处理基于窗口的弹出窗口的第三方工具。

Accepting confirmation popups in Selenium can be handled via Alert object (assuming driver instance is ready): 可以通过Alert对象处理Selenium中接受确认弹出窗口(假设驱动程序实例已准备就绪):

Alert myAlert = driver.switchTo().alert();
myAlert.accept();

Use autoit download the autoit exe record clicking of that button Use the code and execute it via JS executor 使用autoit下载autoit exe记录,单击该按钮即可。使用代码并通过JS执行器执行

Dont forget to place the code before you are actually executing the step not after executing it 不要忘记在实际执行步骤之前而不是在执行步骤之前放置代码

Please switch to windows dialogs first using get window handles eg. 请首先使用获取窗口句柄切换到Windows对话框。 Save As 另存为

Then, use Robot commands to send Enter key. 然后,使用机器人命令发送Enter键。 It will save the file. 它将保存文件。

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

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