{"id":5542,"date":"2021-11-08T13:28:45","date_gmt":"2021-11-08T13:28:45","guid":{"rendered":"https:\/\/www.npntraining.com\/blog\/?p=5542"},"modified":"2021-11-15T16:51:42","modified_gmt":"2021-11-15T16:51:42","slug":"how-to-upload-a-file-with-selenium","status":"publish","type":"post","link":"https:\/\/www.npntraining.com\/blog\/how-to-upload-a-file-with-selenium\/","title":{"rendered":"How To Upload a File with Selenium"},"content":{"rendered":"<p>It is very common functionality to upload a file to the webserver. But when it involves in automating you get a dialog box that is just out of reach for selenium.<\/p>\n<p>In selenium to achieve this functionality people use AutoIt a third party tool to manipulate the dialog box but one major disadvantage of AutoIt works only with windows limiting your ability to test this functionality on different browser &amp; operating system combinations.<\/p>\n<p>A work-around for this problem is to side-step the system dialog box entirely. We can do this by using&nbsp;Selenium&nbsp;to insert the full path of the file we want to&nbsp;upload&nbsp;(as text)&nbsp;into&nbsp;the form and then submit the form.<\/p>\n<pre>package org.npntraining.hands_on;\nimport io.github.bonigarcia.wdm.*;\nimport org.openqa.selenium.*;\nimport org.openqa.selenium.firefox.*;\nimport org.testng.*;\nimport org.testng.annotations.*;\nimport java.io.*;\npublic class FileUpload {\n    @Test\n    public void uploadFile() throws Exception {\n        WebDriverManager.firefoxdriver().setup();\n        WebDriver driver = new FirefoxDriver();\n        driver.get(\"http:\/\/the-internet.herokuapp.com\/upload\");\n        String filename = \"d:\/Dockerfile.txt\";\n        File file = new File(filename);\n        String path = file.getAbsolutePath();\n        driver.get(\"http:\/\/the-internet.herokuapp.com\/upload\");\n        driver.findElement(By.id(\"file-upload\")).sendKeys(path);\n        driver.findElement(By.id(\"file-submit\")).click();\n        String text = driver.findElement(By.id(\"uploaded-files\")).getText();\n        Assert.assertEquals(text, file.getName());\n    }\n}<\/pre>\n<p>This approach will work across all browsers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is very common functionality to upload a file to the webserver. But when it involves in automating you get a dialog box that is just out of reach for&hellip;<\/p>\n","protected":false},"author":1,"featured_media":5713,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61,62],"tags":[],"class_list":["post-5542","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kafka","category-machine-learning"],"_links":{"self":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts\/5542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/comments?post=5542"}],"version-history":[{"count":5,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts\/5542\/revisions"}],"predecessor-version":[{"id":5715,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/posts\/5542\/revisions\/5715"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/media\/5713"}],"wp:attachment":[{"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/media?parent=5542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/categories?post=5542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.npntraining.com\/blog\/wp-json\/wp\/v2\/tags?post=5542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}