简体   繁体   中英

How to upload file from windows using Selenium webdriver

I am trying to upload file using selenium WebDriver,

There is one popwindow which is having 'Browse' button, after click on 'Browse' it opens file from 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");
 }

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");

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

Now the thing is windows and linux/mac is consider slashes / differnently

If you are working on windows then use forward \\ and for unix/mac use back /

Example:-

For windows use below syntax

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

Try also with

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

For mac/linux use below syntax

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

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
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)

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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