【问题标题】:Uploading File Using Selenium使用 Selenium 上传文件
【发布时间】:2012-10-19 07:06:06
【问题描述】:

我是 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 会自动上传并提交文件。

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

【问题讨论】:

  • 您实际上是在尝试单击按钮本身吗?不,不要这样做。您只需要将文件位置发送给元素本身。
  • 是的,我正在尝试点击浏览按钮。
  • selenium.type("document.forms['UploadForm'].elements['button']",file.getAbsolutePath());早些时候我的代码是这样的,我将文件位置发送到元素,它dint wokr。所以现在我需要将调用autoit脚本的对象发送到元素selenium.type(“document.forms ['UploadForm']。元素['button']",proc);

标签: java selenium selenium-rc selenium-ide


【解决方案1】:

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

    InternetExplorerDriver driver = new InternetExplorerDriver();

    //Do stuff obviously ....

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

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 2021-05-24
    • 2019-10-13
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多