【问题标题】:How to upload file using selenium如何使用硒上传文件
【发布时间】:2012-10-17 09:26:00
【问题描述】:

下面是我的硒代码

selenium.type("document.forms['UploadForm'].elements['browse']",file.getAbsolutePath());
selenium.click("document.forms['UploadForm'].elements['submit']");

我有多个表单,其中有多个属性为“broswe”的浏览按钮和多个属性为 submit 的提交按钮。因为我使用需要上传文件的表单名称上传文件,即“UploadForm”并且需要以相同的形式提交。

但我面临的问题是没有上传文件,selenium 正在点击提交按钮。

下面是autoit脚本

WinWaitActive("Choose File")
Send("C:\Work\selenium\PMG_SPRINT_AUTOMATION\pmg_automation\block_PTN.csv")   
Send("{ENTER}")

由于有多个 selenium 按钮,我如何在我的 selenium 类中调整此代码

try {
  String[] commands = new String[]{};
  commands = new String[]{"C:\\Program Files\\AutoIt3\\attachDocScript.exe"}; //location of the autoit executable
  Runtime.getRuntime().exec(commands);
       }
  catch (IOException e) {}

【问题讨论】:

    标签: selenium selenium-rc selenium-ide


    【解决方案1】:

    您可以使用 Autoit。在那里您可以非常轻松地编写用于上传和下载文档的脚本,然后您可以在需要时在 selenium 代码中调用这些脚本。

    【讨论】:

    【解决方案2】:

    您只使用了正确的逻辑。对于上传文件,您需要在浏览区域中输入内容(文件绝对路径)而不点击该内容,然后点击提交上传该文件。

    即使您有多个浏览按钮,您也可以使用索引或特定定位器来指定一个。

    例如:

    selenium.type("//span[@id='attach1']/input","/home/test/file1.csv");
    selenium.type("//span[@id='attach2']/input","/home/test/file2.csv");
    selenium.type("//span[@id='attach3']/input","/home/test/file3.csv");
    .
    .
    selenium.click("submit");
    

    【讨论】:

    • 我有多个浏览按钮名称相同但形式不同。那么我该怎么做呢?
    • selenium.type("//span[@id='document.forms['bulkUnBlockUploadForm'].elements['requestFile']']/input",file.getAbsolutePath()); //即使这个语法改变也不起作用
    【解决方案3】:
    1. 为多个文件上传创建 AutoIT 脚本

      WinWaitActive("Open"); Name of the file upload window
      Send(' C:\Users\himanichaudhary\Desktop\"Capture1.PNG" "stop.jpg" "image3" image4"');
      Send("{ENTER}")
      
    2. 保存并编译脚本以创建“exe”

    3. 在C#代码中使用Process.start("D:\\Users\\MultiFileUpload.exe");(AutoIt exe所在文件的路径,双斜线格式)。**

      Runtime.getRuntime().exec("Path of file where autoIt script in exe is located");`
      

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多