簡體   English   中英

如何處理 Selenium WebDriver 中的文件上傳元素?

[英]How to handle file upload element in Selenium WebDriver?

我正在使用 WebDriver 和 JUnit 在 Selenium 中編寫腳本以在服務器上上傳文件。 但無法做到這一點,因為我不知道如何處理 Windows(OS)的文件資源管理器窗口顯示在單擊文件上傳元素的選擇文件按鈕時。

我試過直接在文本框中輸入文件路徑,然后點擊上傳按鈕。 但它仍然顯示請選擇要上傳的文件。

下面是元素的HTML:

<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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM