gpt4 book ai didi

java - 使用 Selenium 在 IE 中下载文件

转载 作者:搜寻专家 更新时间:2023-11-01 02:38:04 25 4
gpt4 key购买 nike

好的,所以我正在尝试使用 Selenium 导出文件。我的浏览器是IE。当我单击导出按钮时,会出现一个 native Windows 对话框。

弹出窗口的图像 enter image description here

我必须单击“保存”按钮。为此,我尝试使用 AutoIT 但它不起作用。

    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,因为这也将使浏览器能够保存文件。那也不管用。

   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() 之后打印一行,但它也没有打印出来。

我是新人所以我不明白这个问题。请帮帮我。

最佳答案

因此,问题是当您调用 click() 函数时,光标有时会卡住。因此,作为解决方案,我使用 Robot 类移动光标并单击导出按钮,然后我使用 Robot 类按 Alt+S,这是在 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();
}

为了获取上述代码片段中的坐标,我使用了以下行

Point coordinates = driver.findElement(By.id("id")).getLocation();
System.out.println("Co-ordinates"+coordinates);

然后按 Alt+S 我使用了以下代码

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();
}

关于java - 使用 Selenium 在 IE 中下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41695031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com