使用Selenium在IE中下载文件

编程入门 行业动态 更新时间:2024-10-27 18:22:28
本文介绍了使用Selenium在IE中下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好,所以我正在尝试使用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(); }

我认为Web驱动程序存在一些问题,因为我尝试在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中下载文件

本文发布于:2023-06-04 22:09:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/506076.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   Selenium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!