使用Selenium上传文件

时间:2012-10-19 07:06:06

标签: java selenium selenium-rc selenium-ide

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

单击浏览按钮的脚本:

#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会自动上传,文件会被提交。

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

1 个答案:

答案 0 :(得分:1)

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

中的内容相差无几
    InternetExplorerDriver driver = new InternetExplorerDriver();

    //Do stuff obviously ....

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