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, 2019 in Selenium by Jigyasa
81 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, 2019 by Anvi
• 14,010 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer

Not able to upload a file in Selenium Webdriver using python Scripting

Hey Srinivas, you can checkout this code ...READ MORE

answered Sep 10, 2019 in Selenium by Abha
• 27,800 points
766 views
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,370 points
1,193 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,510 points
2,215 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,870 points
1,823 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,870 points
1,818 views
0 votes
1 answer

How to extract text from a web page using selenium and save it as a text file?

Hello Isha, you can checkout this code ...READ MORE

answered May 7, 2019 in Selenium by Anvi
• 14,010 points
11,252 views
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, 2019 in Selenium by Anvi
• 14,010 points
1,622 views