0

有元素的页面:

<div class="block-title-inside">
     <a href="javascript:void(0);" class="block-title-link fright" style="display:block; overflow: hidden; width: 105px;">Upload video <span class="blue-arrow-right"></span>
     <input type="file" name="file" id="videoUpload" class="dpt-mediafile-input-button" style="z-index: 999"></a>
Videos</div>

没有“附加”按钮,因此在单击上面显示的“上传视频”按钮时,用户会看到本机操作系统窗口,可以选择要上传的文件。

我无法使用 selenium WD 和简单的代码上传文件,例如不起作用:

driver.findElement(By.id("videoUpload")).sendKeys("D://Other//sample_videos//barsandtone.FLV");
driver.findElement(By.id("videoUpload")).click();

问题似乎是该站点使用 jQuery 上传脚本。以下是相关的 JS 代码部分:

  1. http://www.speedyshare.com/DEXEQ/main.js(来宾用户看不到脚本本身)
  2. 两个链接,请分开:__http://www.dailypreptalk.com/components/com_dpt/assets/file-upload/js/jquery.fileupload.js">http://www.dailypreptalk.com/components/com_dpt/assets /file-upload/js/vendor/jquery.ui.widget.js_ _ _http ://www.dailypreptalk.com/components/com_dpt/assets/file-upload/js/jquery.fileupload.js

我试图运行js代码:

js.executeScript("document.getElementById('videoUpload').value = 'D://Other//sample_videos//barsandtone.FLV'");
js.executeScript("document.getElementById('videoUpload').click()");

但还是没有结果。我还尝试使用 jQuery 代码初始化窗口,但也没有机会:

js.executeScript("dpt.jQuery('#videoUpload').value = 'D://Other//sample_videos//barsandtone.FLV'");
js.executeScript("dpt.jQuery('#videosModal').modal('show');");

我只是不知道如何使用预填充的文件路径初始化 jQuery 上传小部件。

谢谢你的帮助!

4

1 回答 1

0

使用机器人功能找到了解决方案:

import java.awt.*;
import java.awt.event.KeyEvent;

原始代码 + D:/1.avi 的文件路径:

driver.findElement(By.id("videoUpload")).click();
  driver.getWindowHandle();
  //type path to your file using robot VK language
  try {
      Robot r = new Robot();
      r.keyPress(KeyEvent.VK_D); //D char
      r.keyPress(KeyEvent.VK_SHIFT); // colon char
      r.keyPress(KeyEvent.VK_SEMICOLON); // colon char
      r.keyRelease(KeyEvent.VK_SEMICOLON); // colon char 
      r.keyRelease(KeyEvent.VK_SHIFT); // colon char
      r.keyPress(KeyEvent.VK_BACK_SLASH); // backslash char etc
      r.keyPress(KeyEvent.VK_1);
      r.keyPress(KeyEvent.VK_PERIOD);
      r.keyPress(KeyEvent.VK_A);
      r.keyPress(KeyEvent.VK_V);
      r.keyPress(KeyEvent.VK_I);
      r.keyPress(KeyEvent.VK_ENTER);
  } catch (AWTException e) {
      e.printStackTrace();
  }
于 2013-09-30T22:10:52.593 回答