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
858 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,150 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
• 28,140 points
4,181 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,360 points
2,857 views
0 votes
2 answers

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,500 points
13,607 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,104 views
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,910 points
7,200 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,852 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,150 points
33,702 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,150 points
6,504 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP