繁体   English   中英

如何使用Selenium Webdriver从Windows上传文件

[英]How to upload file from windows using Selenium webdriver

我正在尝试使用Selenium WebDriver上传文件,

有一个弹出窗口带有“浏览”按钮,单击“浏览”后,它将从Windows打开文件。

http://i.stack.imgur.com/wHYES.png

{
      //Switch focus on pop window
      String popupHandle = driver.getWindowHandle();
      WebDriver popup;
      popup = driver.switchTo().window(popupHandle);

      //Click on 'Browse Files' button.
      Thread.sleep(5000);
      WebElement filebrowse = driver.findElement(By.id("browse-files"));
      filebrowse.sendKeys("C:/Users/logo.jpg");
 }

我想上传单个和多个文件,是否还有其他方法可以从窗口中选择多个文件?

filebrowse.sendKeys(“ file:// C:/Users/logo.jpg”);

您需要提供绝对路径。

我检查了它的工作情况。

您的代码看起来不错。 我假设您的定位器能够识别该按钮,请先检查以关闭弹出窗口,以确保Windows控制器已正确切换到弹出窗口

现在,问题是Windows和linux / mac被认为是斜杠/

如果您在Windows上工作,则使用forward \\ ;对于unix / mac,请使用back /

例:-

对于Windows,请使用以下语法

filebrowse.sendKeys("C:\Users\logo.jpg");

也可以尝试

filebrowse.sendKeys("C:\\Users\\logo.jpg");

对于Mac / Linux,请使用以下语法

filebrowse.sendKeys("C:/Users/logo.jpg");

要么

filebrowse.sendKeys("C://Users//logo.jpg");

如果仍然无法正常工作,则最好发布您的HTML代码,以便我们也可以检查您的定位器

希望它能对您有所帮助:)

Selenium Webdriver不支持处理Windows对话,因为有第三方工具(如Sikuli和AutoIt)可用
为您(通过查看您的图像)有两种选择

  1. 使用文本框上传
  2. 使用Windows对话框(为此,您需要使用诸如AutoIt,Sikuli之类的工具)

对于文本框,首先需要检查是否已启用键入任何文本,然后传递文件的路径。

Driver.findElement(by.id("")).sendKeys("path of file");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM