Selenium WebDriver - 无法使用AutoIT

时间:2015-05-15 01:32:49

标签: java selenium file-upload selenium-webdriver 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");

对于附加链接,当我使用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");

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

这是因为在您的Html文件中输入类型=文件。如果您的输入类型是其他内容,则需要尝试使用AutoIt或RobotApi。