使用 selenium 和 AutoIT 从 windows 探索中自动上传图像

automate image upload from windows explore with selenium and AutoIT

我必须使用 Selenium 自动执行一个场景,例如将图像从 windows 资源管理器上传到 TinyMCE 组件。由于Selenium不支持OS控件,所以我使用AutoIt上传图片。 AutiIt 将图像路径设置为 "File name" 并且它似乎也单击了 'Open' 按钮。但是图像没有加载到我的 "source" 字段中。

这是我的 Selenium 命令:

Runtime.getRuntime().exec("C:/XXXXX/src/test/resources/uploadImage.exe");
// Path of the AutoIT script file

AutoIT 脚本:

WinActivate("Open")
Send("C:\LCNGProjects\Screenshots\Images\GifImage1.gif")
Sleep(5000)
Send("{ENTER}")

参考附件截图

不确定我是否答对了问题,你是否编译了脚本。

您能否通过下面的 link 了解脚本编译和如何上传文件的分步信息。

https://www.guru99.com/use-autoit-selenium.html

您不需要使用 AutoIt。自动将使您的脚本仅在 windows 机器上可靠。

而不是使用标签为 type="file" 的上传按钮的定位器(即 xpath)查找元素。现在传递这个简单的 sendKeys 和你机器中图像的绝对路径,如下所示:-

button2.sendKeys("C:\UsersDesktop\logo\Summit-Logo-900px.png");

或使用

System.getProperty("user.dir")); 

在你的项目中追加文件路径,排除你的项目路径和文件名 示例:-

button.sendKeys(System.getProperty("user.dir")+"\logo\Summit-Logo-900px.png");

下方 link 包含更多上传文件的选项,如下所示:-

  • 使用 SendKeys 命令。
  • 使用 AutoIt 脚本。
  • 使用 Jacob com 界面。

http://www.techbeamers.com/handle-file-upload-selenium-webdriver/#h3

希望对您有所帮助:)

我不打算在这里添加完整的源代码。您可以在测试用例中使用此命令调用 AutoIT 脚本。

// Call to AutoIT script to upload the image

Runtime.getRuntime().exec("C:\xxxxx\src\test\resources\uploadImage.exe");

AutoIT 脚本

 ControlFocus("Open","","Edit1")
    ControlSetText("Open","","Edit1","C:\xxxx\Images\jpgImage")
    ControlClick("Open","","Button1")

有关更多信息,请参阅以下内容link https://www.guru99.com/use-autoit-selenium.html