使用 Selenium 在 IE 中下载文件 [英] Download a file in IE using Selenium
问题描述
好的,所以我正在尝试使用 Selenium 导出文件.我的浏览器是IE.当我单击导出按钮时,会出现一个本机 Windows 对话框.
OK, so I am trying to export a file using Selenium. My browser is IE. When I click on the export button a native windows dialogue box comes up.
弹出窗口的图像
我必须点击保存按钮.为此,我尝试使用 AutoIT
但它不起作用.
I have to click on the Save button. For this I tried using AutoIT
but its not working.
exportbutton.click();
Thread.sleep(2000);
driver.switchTo().activeElement();
AutoItX x = new AutoItX();
x.winActivate("window name");
x.winWaitActive("window name");
x.controlClick("window name", "", "[CLASS:Button; INSTANCE:2]");
这没有用.所以我决定使用 Robot 类并执行键盘点击 Atl + S
,因为这也将使浏览器能够保存文件.那也没有用.
This did not work. So I decided to use Robot class and perform the keyboard clicks Atl + S
, as this will also enable the browser to Save the file. That did not work either.
try
{
Robot robot = new Robot();
robot.setAutoDelay(250);
robot.keyPress(KeyEvent.VK_ALT);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
}
catch (AWTException e)
{
e.printStackTrace();
}
我认为网络驱动程序存在一些问题,因为我尝试在 exportbutton.click()
之后打印一行,但也没有打印出来.
There is some problem with the web driver I suppose because I tried printing a line after exportbutton.click()
and it did not get printed either.
我是新手,所以我无法理解这个问题.请帮帮我.
I am new so I can't understand the problem. Please help me out.
推荐答案
所以,问题是有时调用 click() 函数时光标会卡住.因此,作为解决方案,我使用 Robot 类移动光标并单击导出按钮,然后使用 Robot 类按 Alt+S,这是在 IE 中保存文件的键盘快捷键.
So, the problem was that the cursor gets stuck sometimes when you call the click() function. So as a solution I used the Robot class to move my cursor and click on the export button and then I used Robot class to press Alt+S, which is a keyboard shortcut to save a file in IE.
点击我使用的按钮
try
{
Robot robot = new Robot();
Thread.sleep(2000);
robot.mouseMove(coordinates.getX()+100,coordinates.getY()-400);
Thread.sleep(2000);
robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
catch (AWTException e)
{
e.printStackTrace();
}
为了获取上面代码片段中的坐标,我使用了以下行
To get the coordinates in the above snippet I used the following line
Point coordinates = driver.findElement(By.id("id")).getLocation();
System.out.println("Co-ordinates"+coordinates);
然后按 Alt+S 我使用了以下代码
And to press Alt+S I used the following code
try
{
Robot robot = new Robot();
robot.setAutoDelay(250);
robot.keyPress(KeyEvent.VK_ALT);
Thread.sleep(1000);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_S);
}
catch (AWTException e)
{
e.printStackTrace();
}
这篇关于使用 Selenium 在 IE 中下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!