selenium - 利用AutoIt实现文件上传

本指南将以简单的方式向您展示如何使用Autoit Step by Step指南在Selenium Webdriver中上传文件。

  • 虽然自动化Web的应用程序很多时候你会得到基于窗口的活动喜欢-文件上传,文件下载弹出,窗口认证的安全网站等。

  • 在这种情况下,Selenium将失败并且无法处理桌面元素以避免这种情况,我们将使用AutoIT脚本来处理桌面或Windows元素,并将AutoIT脚本与我们的Selenium代码结合起来。

AutoIt 工具介绍:

  1. AutoIt是免费的自动化工具,可以与桌面应用程序一起工作。

  2. 它使用击键,鼠标移动和窗口/控制操作的组合,以使用其他语言(例如VBScript和SendKeys)不可能或不可靠的方式自动执行任务。

有关AutoIT的更多信息,请访问官方网站 AutoIt官​​方网站

一、 首先从下载开始

第1步 · 导航到AutoIt官​​方网站 https://www.autoitscript.com/site/autoit/downloads/ 并进入下载部分或点击此处下载AutoIt

第2步 · 点击下载AutoIt并安装

第3步 · 点击下载编辑器并安装。

selenium - 利用AutoIt实现文件上传_第1张图片
image.png

第4步 · 一旦安装在您的机器检查所有安装正确。

注 - 通常,如果不更改它,它将转到C:\ Program Files \ AutoIt3位置


selenium - 利用AutoIt实现文件上传_第2张图片
image.png

第5步 · 打开SCiTE文件夹并点击SciTE,这将打开AutoIt编辑器

selenium - 利用AutoIt实现文件上传_第3张图片
image.png

一旦完成安装让我们看看我们如何编写脚本

使用Autoit在Selenium Webdriver中上传文件

  • 要使用Autoit在Selenium Webdriver中上传文件,我们需要注意一些步骤,让我们开始吧
  • 要在Selenium Webdriver中上传文件,我们将创建AutoIT脚本,它将处理文件上传的窗口,然后我们将Selenium脚本与AutoIt脚本结合起来
第1步 · 打开编辑器和查找工具
  • 查找工具 Au3Info.exe


    selenium - 利用AutoIt实现文件上传_第4张图片
    image.png
  • 编译器 SciTE.exe


    selenium - 利用AutoIt实现文件上传_第5张图片
    image.png
第2步 · 我们需要编写脚本来上传文件,因此我们将使用AutoIt的一些方法。每种方法都有一些自己的功能
  • ControlFocus - 这将使窗口焦点
  • ControlSetText - 这将设置文件路径
  • ControlClick - 这将点击按钮

1. 点击浏览按钮,一个新的窗口将打开现在开放的查找工具,并点击查找工具,并拖动到文件名,如下图所示。

selenium - 利用AutoIt实现文件上传_第6张图片
image.png

2. 这将给出关于该窗口和文件名段信息的所有细节; 我们将只使用一些属性,如窗口标题,类和实例
打开AutoIt编辑器和写脚本
selenium - 利用AutoIt实现文件上传_第7张图片
image.png

在ControlClick方法中,我们将提供打开按钮的控件ID

第3步 · 将脚本保存到具有某个唯一名称的特定位置。
  • 注: 默认情况下,脚本将被保存为.au3扩展名


    selenium - 利用AutoIt实现文件上传_第8张图片
    image.png
第4步 · 现在编译脚本,以便编译右键单击文件并选择编译脚本,这将生成文件的.exe文件。
selenium - 利用AutoIt实现文件上传_第9张图片
image.png
第5步 ·现在编写Selenium程序并添加此.exe文件并运行您的程序
package demo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class DemoFileUpload {

 public static void main(String[] args) throws Exception {
                
// This will open Firefox browser
WebDriver driver=new FirefoxDriver();
        
// This will maximize browser to full screen
driver.manage().window().maximize();
        
// This will open respective URL
driver.get("your application url");
        
// This will click on Upload button
driver.findElement(By.xpath("//*[@type='file']")).click();
     
// This will invoke AutoIT script here give the path of the script 
//and this will throw IO exception so u can use throw or try catch
// In my case I am using throws
Runtime.getRuntime().exec("C:\\Users\\mukesh_otwani\\Desktop\\AutoItScripts\\blogUpload.exe");

// Once you will run this program AutoIt script will be invoked and respective f//ile will be attached
  
    }
}

你可能感兴趣的:(selenium - 利用AutoIt实现文件上传)