gpt4 book ai didi

java - 使用 Selenium 上传文件

转载 作者:行者123 更新时间:2023-12-01 15:05:51 27 4
gpt4 key购买 nike

我是 selenium 的新手。我的要求是使用浏览按钮自动上传 csv 文件。我面临的问题是我有 2 个带有两个浏览按钮的表单,具有相同的名称和相同的值。所以我必须单击浏览基于表单的按钮(表单名称不同)。下面是我的示例代码

点击浏览按钮的脚本:

#include <IE.au3>
; Internet Explorer is partly integrated in shell.application
$oShell = ObjCreate("shell.application") ; Get the Windows Shell Object
$oShellWindows=$oShell.windows ; Get the collection of open shell Windows
$MyIExplorer=""
for $Window in $oShellWindows ; Count all existing shell windows
; Note: Internet Explorer appends a slash to the URL in it's window name
if StringInStr($Window.LocationURL,"http://") then
$MyIExplorer=$Window
exitloop
endif
next
$oForm = _IEGetObjByName ($MyIExplorer, "document.forms['UploadForm'].elements['browsebutton']")
_IEAction($oForm, "click")

下面是我上传 csv 文件的脚本

WinActivate("File Upload");
Local $file ="C:\Work\selenium\abc.csv"
ControlSetText("Choose file", "", "Edit1", $file )
ControlClick("File Upload", "", "Button2")

我在我的java类中调用如下代码:

Process proc = Runtime.getRuntime().exec("C:\\bowsebutton.exe");
Process proc1 = Runtime.getRuntime().exec("C:\\test3.exe");

当我运行 seleinum 时,根本无法单击浏览按钮。但是如果我手动单击浏览按钮,csv 会自动上传并提交文件。

我无法弄清楚为什么没有单击浏览按钮。任何帮助将不胜感激。

最佳答案

您必须使用 FindElement 来弹出对话框。我不知道它在 Java 中是什么样子,但它与 C# 中的情况相差不会太远

    InternetExplorerDriver driver = new InternetExplorerDriver();

//Do stuff obviously ....

driver.FindElement(By.XPath("//input[@type='file']")).SendKeys(@"c:\somefile.txt");

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

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