gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:12:05 26 4
gpt4 key购买 nike

我无法使用 AutoIT v3 上传文件

尝试了很多方法,但没有成功

方法一:(获取元素当前不可见)

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

方法二:(获取元素当前不可见)

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

方法三:(通过:testCaseOne,但没有上传文件)

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");

最佳答案

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

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

关于java - Selenium WebDriver - 无法使用 AutoIT 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30250140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com