Run This code :
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1])",` driver.findElement(By.xpath("//input[@type='file']")), "0");
js.executeScript("arguments[0].setAttribute('class', arguments[1])", driver.findElement(By.xpath("//input[@type='file']/../../div[2]")), "a");
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("Your Path to the file your system");
Explanation: Every Browse button has a <input>
tag in DOM in hidden state . By using the below lines of code, we just change the class and style attributes of the tags enclosing that <input>
tag so that it becomes visible and a sendKeys()
command can be performed on it. After that when you do a sendKeys
with the absolute path of the image/file you want to upload.