简体   繁体   中英

How to handle file upload element in Selenium WebDriver?

I am writing a script in Selenium using WebDriver and JUnit to upload files on server. But unable to do it because I don't know how to handle file explorer window of Windows(OS) shows on the click of choose file button of file upload element.

I have tried it by enter the file path directly in the text box and then click upload button. But still it shows please choose a file to upload.

Below is the HTML of element:

<div class="form-group">   
  <label for="file">File to upload</label>
          <input type="file"  name="image" required />
  <input type="hidden" class="form-control" placeholder=""  name="failure" value="/error"/>
  <input type="hidden" class="form-control" placeholder=""  name="success" value="/success" />
          <input type="hidden" class="form-control" placeholder=""  name="keyword" value=""/>
          <input type="hidden" class="form-control" placeholder=""  name="category" value="" />
          
</div> 
  <input type="submit" value="upload" class="btn btn-primary btn-lg" disabled />
</fieldset>
You can use this

//Actions act= new Actions(driver);
WebElement choose=driver.findElement(By.xpath("//input[@id='choose_file']"));

//act.moveToElement(choose).perform();
Thread.sleep(2000);
choose.sendKeys("D:\\DocLib\\Aadhaar\\1 Aamir.jpg");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM