本文介紹了使用 selenium 上傳文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
如何使用 selenium webdriver 通過窗口提示從本地上傳文件?
How to upload files from local via window prompt using selenium webdriver?
我想執行以下操作:
- 點擊窗口上的瀏覽"選項
- 從窗口提示轉到保存文件的本地特定位置
- 選擇文件并點擊打開"以上傳文件.
推薦答案
我使用了以下三種不同的方式在 selenium webdriver 中上傳文件.
I have used below three different ways to upload a file in selenium webdriver.
- 第一個簡單的例子是找到元素并在其中輸入文檔的絕對路徑.但我們需要確保 HTML 字段是輸入類型.
例如:<input type="file" name="uploadsubmit">
下面是簡單的代碼:
WebElement element = driver.findElement(By.name("uploadsubmit"));
element.sendKeys("D:/file.txt");
driver.findElement(By.name("uploadSubmit"));
String validateText = driver.findElement(By.id("message")).getText();
Assert.assertEquals("File uploaded successfully", validateText);
第二種情況是使用Robot類上傳,該類用于(生成本機系統輸入事件)控制鼠標和鍵盤.
Second case is uploading using Robot class which is used to (generate native system input events) take the control of mouse and keyboard.
另一種選擇是使用AutoIt"(開源工具).
The the other option is to use 'AutoIt' (open source tool).
您可以找到以上三個示例:- 使用 Selenium Webdriver 上傳文件
You can find the above three examples : - File Uploads with Selenium Webdriver
這篇關于使用 selenium 上傳文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!