简体   繁体   English

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

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

I am trying to upload file using selenium WebDriver, 我正在尝试使用Selenium WebDriver上传文件,

There is one popwindow which is having 'Browse' button, after click on 'Browse' it opens file from windows. 有一个弹出窗口带有“浏览”按钮,单击“浏览”后,它将从Windows打开文件。

http://i.stack.imgur.com/wHYES.png 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");
 }

I want to upload single as well as multiple files,Is there any other way to select multiple file from window? 我想上传单个和多个文件,是否还有其他方法可以从窗口中选择多个文件?

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

You need to provide absolute path. 您需要提供绝对路径。

I checked it's working. 我检查了它的工作情况。

Your code looks fine. 您的代码看起来不错。 I am assuming that your locator is able to identify the button and please check first to close your pop-up that windows controller is shifted to pop-up properly 我假设您的定位器能够识别该按钮,请先检查以关闭弹出窗口,以确保Windows控制器已正确切换到弹出窗口

Now the thing is windows and linux/mac is consider slashes / differnently 现在,问题是Windows和linux / mac被认为是斜杠/

If you are working on windows then use forward \\ and for unix/mac use back / 如果您在Windows上工作,则使用forward \\ ;对于unix / mac,请使用back /

Example:- 例:-

For windows use below syntax 对于Windows,请使用以下语法

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

Try also with 也可以尝试

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

For mac/linux use below syntax 对于Mac / Linux,请使用以下语法

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

OR 要么

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

If still not work then it is good to post your HTML code so we can check your locator as well 如果仍然无法正常工作,则最好发布您的HTML代码,以便我们也可以检查您的定位器

Hope it will help you :) 希望它能对您有所帮助:)

Handling windows dialogues is not supported by selenium webdriver, for that there are third party tools available Like Sikuli and AutoIt Selenium Webdriver不支持处理Windows对话,因为有第三方工具(如Sikuli和AutoIt)可用
for you(By looking at your image) have two choices 为您(通过查看您的图像)有两种选择

  1. Upload using textbox 使用文本框上传
  2. Using windows dialogues(for this you need to use tools like : AutoIt,Sikuli) 使用Windows对话框(为此,您需要使用诸如AutoIt,Sikuli之类的工具)

for text box first you need to check is that enabled to type any text, then pass the path of the file. 对于文本框,首先需要检查是否已启用键入任何文本,然后传递文件的路径。

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

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

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