Selenium WebDriver - 无法使用 AutoIT 上传文件

Selenium WebDriver - unable to upload file with AutoIT

我无法使用 AutoIT v3 上传文件

尝试了很多方法,但运气不好

方法一:(get Element is not currently visible)

driver.findElement(By.xpath("//input[@type='file']")).click();
Runtime.getRuntime().exec("D:\Documentation\Script To Upload File.exe");

方法二:(get Element is not currently visible)

driver.findElement(By.name("fileName")).click();
Runtime.getRuntime().exec("D:\Documentation\Script To Upload File.exe");

方法三:(PASSED: testCaseOne, but no file is uploaded)

driver.findElement(By.id("button2")).click();   
Runtime.getRuntime().exec("D:\Documentation\Script To Upload File.exe");

对于附加 link,当我检查 firepath 时,它将引用具有 1 个匹配节点

的 xpath "html/body/input"

这里是我的 html 文件

<input type="file" name="fileName" style="position: absolute; margin: -5px 0px 0px -175px; padding: 0px; width: 220px; height: 30px; font-size: 14px; opacity: 0; cursor: pointer; display: none; z-index: 2147483583; top: 457px; left: 459px;"/>

这是文件上传部分的 html 代码:

<html>
    <body>
        <div>
            <div>
                <form>
                    <div>
                        <dl>
                            <dd class="attachFile">
                                <div class="attachUpload">
                                    <a id="button2" class=" ">
                                        <img class="attachIco" alt="" src="http://qa.seleniumqa.com/ga/en/clean/images/BLANK.GIF"/>
                                            Attach file
                                    </a>
                                </div>
                            </dd>
                        </dl>
                    </div>
                </form>
            </div>
        </div>
    </body>
</html>

上传 File.au3 代码的脚本

; It will wait for 8 seconds to appear File Upload dialog.
; Used Title property of File upload dialog window.

  WinWait("File Upload","",8)

; Set control focus to File name Input box of File Upload dialog.
; Used Class property of File upload dialog window and Class+Instance property for File name Input box.

  ControlFocus("[CLASS:#32770]","","Edit1")

  Sleep(3000)

; Set the name of file In File name Edit1 field.
; "Test.txt" file Is located In AutoIT folder of E drive. So we have to provide full path like E:\AutoIT\Test.txt.

  ControlSetText("[CLASS:#32770]", "", "Edit1", "D:\Documentation\uploadFile.xls")

  Sleep(3000)

; Click on the Open button of File Upload dialog.

  ControlClick("[CLASS:#32770]", "","Button1");

编辑:与将来需要的人分享解决方案

此代码无效(不确定原因)

driver.findElement(By.id("button2")).sendKeys("D:\Documentation\uploadFile.xls");

但以这种方式,它起作用了

driver.findElement(By.id("button2")).click();
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("D:\Documentation\uploadFile.xls");

输入控件的类型是文件,因此,使用下面的行上传文件应该可以。

WebElement e = driver.findElement(By.id("the id"));
e.sendKeys("file path");

那是因为在您的 Html 文件中 input type = file。 如果您的输入类型是其他类型,您将需要尝试使用 AutoIt 或 RobotApi。