How to upload a file to a server using Selenium Webdriver?

0 votes
I want to write an automation script which would upload a file to a browser, using Selenium Webdriver. Can anyone please help?
May 20 in Selenium by Jigyasa
45 views

1 answer to this question.

0 votes

Hi Jigyasa, following piece of code will give you an idea how you can upload a file to server using Selenium Webdriver:

public class UploadFile{

 public static void main(String[] args) {

  System.setProperty("webdriver.gecko.driver","C:\\Users\\Anvi_R\\Downloads\\ExtractedFiles\\geckodriver-v0.24.0-win64\\geckodriver.exe");  
  WebDriver driver = new FirefoxDriver();
 
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
 
  driver.manage().deleteAllCookies();
  driver.manage().window().maximize();
 
  driver.get("http://www.tinyupload.com/");
  
  // search the browse button
  WebElement uploadElement = driver.findElement(By.className("pole_plik"));
  
     // click the browse button and enter the file path
     uploadElement.sendKeys("C:\\Users\\Anvi_R\\Desktop\\Q1.png");
    
     // click the "UploadFile" button
     driver.findElement(By.xpath("/html/body/table/tbody/tr[4]/td/table/tbody/tr/td[2]/form/table/tbody/tr[2]/td[1]/img")).click();

 }
}
answered May 21 by Anvi
• 13,400 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to upload a resume to a website using selenium in python?

Use this code, this will help you: from ...READ MORE

answered Apr 20, 2018 in Selenium by Vardy
• 2,250 points
1,074 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
767 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,011 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
719 views
0 votes
1 answer
0 votes
1 answer

How to mouse hover on a web element using Selenium WebDriver?

Hello Nitin, to automate mouse hovering over ...READ MORE

answered May 7 in Selenium by Anvi
• 13,400 points
230 views