{"id":3087,"date":"2020-05-04T18:36:03","date_gmt":"2020-05-04T13:06:03","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=3087"},"modified":"2026-01-13T06:53:53","modified_gmt":"2026-01-13T11:53:53","slug":"file-upload-and-download-using-selenium-webdriver","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/file-upload-and-download-using-selenium-webdriver\/","title":{"rendered":"File Upload and Download using Selenium Webdriver"},"content":{"rendered":"\n<p>In this article, we will learn how to handle file uploads and downloads.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Uploading Files<\/strong><\/h2>\n\n\n\n<p>To Upload files, we will use as our test application. Through this site any user can upload files without requiring them to sign up.<br>In <strong>Selenium WebDriver<\/strong>, we can upload files by simply using the <strong>sendKeys()<\/strong> method on the select file input field to enter the path to the file to upload. Mastering this technique is crucial for your <strong><a href=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\" data-type=\"link\" data-id=\"https:\/\/www.h2kinfosys.com\/courses\/selenium-automation-testing-certification-course\/\">Selenium Certification<\/a><\/strong>, as it demonstrates your ability to automate file upload functionality in web applications, a key skill for any Selenium expert.<\/p>\n\n\n\n<p>In real-world automation projects, <strong>File Upload<\/strong> scenarios often appear in forms, profile pages, and document management systems. Understanding how Selenium handles <strong>File Upload<\/strong> without relying on OS pop-ups helps testers build stable and cross-platform test suites. This approach makes <strong>File Upload<\/strong> automation faster, more reliable, and easier to maintain in CI\/CD pipelines.<\/p>\n\n\n\n<p>Beyond basic examples, File Upload tests should validate success messages, uploaded file names, and error handling for unsupported formats. When combined with proper waits and assertions, File Upload automation ensures end-to-end coverage of critical user workflows. Practicing File Upload use cases strengthens your confidence in handling complex enterprise applications.<\/p>\n\n\n\n<p>Let&#8217;s create a test case in which we will automate the following scenarios to handle File-Upload:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Invoke a Google chrome browser.<\/li>\n\n\n\n<li>Open URL: http:\/\/the-internet.herokuapp.com\/upload<\/li>\n\n\n\n<li>Click on Choose file to upload a file.<\/li>\n\n\n\n<li>Click on Upload button<\/li>\n<\/ul>\n\n\n\n<p>Now, we will create a test case step by step in order to understand of how to handle file-upload in WebDriver.<\/p>\n\n\n\n<p><strong>Step 1:<\/strong> Launch Eclipse IDE.<\/p>\n\n\n\n<p><strong>Step 2:<\/strong> Go to File &gt; New &gt; Click on Java Project.<\/p>\n\n\n\n<p>Enter Project Name: Demo_FileUpload<\/p>\n\n\n\n<p><strong>Step 3:<\/strong> Right click on the Project Name and click on the New &gt; class.<\/p>\n\n\n\n<p>Give your Class name as &#8220;Test_Fileupload&#8221; and Select the checkbox \u201cpublic static void main(String[] args) and click on &#8220;Finish&#8221; button.<\/p>\n\n\n\n<p><strong>Step 4:<\/strong> Invoke the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Google_Chrome\" rel=\"nofollow noopener\" target=\"_blank\">Google Chrome browser<\/a> and set the system property to the path of your chromedriver.exe file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;box type=\"info\" align=\"\" class=\"\" width=\"\"]Here is the sample code to set Chrome driver system property:\n\n\/\/ System Property for Chrome Driver&nbsp;&nbsp;&nbsp;\n\n&nbsp;System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");&nbsp; &#91;\/box]<\/code><\/pre>\n\n\n\n<p>After that we have to initialize the Chrome driver using ChromeDriver Class. Below is the sample code to initialize Chrome driver using ChromeDriver class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;box type=\"info\" align=\"\" class=\"\" width=\"\"]\/\/ Instantiate a ChromeDriver class.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n\nWebDriver driver=new ChromeDriver();&nbsp; &#91;\/box]<\/code><\/pre>\n\n\n\n<p>We will get the below code to launch Google Chrome browser after combining both of the above codes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;box type=\"info\" align=\"\" class=\"\" width=\"\"]System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");&nbsp;&nbsp;\n\nWebDriver driver=new ChromeDriver();&#91;\/box]<\/code><\/pre>\n\n\n\n<p>After that, we need to navigate to the desired URL.<\/p>\n\n\n\n<p>Below is the sample code to navigate to the desired URL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;box type=\"info\" align=\"\" class=\"\" width=\"\"]\/\/ Launch Website&nbsp;&nbsp;\n\ndriver.navigate().to(\"http:\/\/the-internet.herokuapp.com\/upload\"); &#91;\/box]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Here is the complete code for above scenario:<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.openqa.selenium.WebDriver;&nbsp;&nbsp;\nimport org.openqa.selenium.chrome.ChromeDriver;&nbsp;&nbsp;\n&nbsp;&nbsp;\npublic class Test_Fileupload {&nbsp;&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;public static void main(String&#91;] args) {&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/ System Property for Chrome Driver&nbsp;&nbsp;&nbsp;\n&nbsp;System.setProperty(\"webdriver.chrome.driver\", \u201c D:\\\\Drivers\\\\geckodriver.exe \");&nbsp;&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;String baseUrl = (\"http:\/\/the-internet.herokuapp.com\/upload\");&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;WebDriver driver=new ChromeDriver();&nbsp;&nbsp;\n&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;driver.get(baseUrl);\n&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;\n}&nbsp;<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow\">\n<pre class=\"wp-block-preformatted\"><br><\/pre>\n<\/div><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will learn how to handle file uploads and downloads. Uploading Files To Upload files, we will use as our test application. Through this site any user can upload files without requiring them to sign up.In Selenium WebDriver, we can upload files by simply using the sendKeys() method on the select file [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":3091,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[156,690,689,691,448,51],"class_list":["post-3087","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-selenium-tutorials","tag-automation-testing","tag-download","tag-file-upload","tag-handle-file-upload","tag-selenium-webdriver","tag-software-testing"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3087","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=3087"}],"version-history":[{"count":3,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3087\/revisions"}],"predecessor-version":[{"id":34157,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/3087\/revisions\/34157"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/3091"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=3087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=3087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=3087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}