[英]How to upload multiple files using selenium without the upload popup dialog?
[英]Upload files using selenium
如何從本地通過窗口提示使用硒的webdriver上傳文件?
我要執行以下操作:
您是否嘗試在適當的文件輸入控件上使用input()
?
WebElement fileInput = driver.findElement(By.id("some id"));
fileInput.sendKeys("C:/path/to/file.extension");
我已經使用以下三種不同的方式在Selenium Webdriver中上傳文件。
Ex:<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類上載,該類用於(生成本地系統輸入事件)控制鼠標和鍵盤。
另一個選擇是使用“ AutoIt”(開源工具)。
您可以找到上述三個示例:- 使用Selenium Webdriver上傳文件
Selenium Webdriver並不真正支持此功能。 在WebDriver討論板上,與非瀏覽器窗口進行交互(例如本機文件上傳對話框和基本的auth對話框)已經成為很多討論的主題,但是在該主題上幾乎沒有進展。
過去,我可以通過使用諸如Fiddler2之類的工具捕獲基礎請求,然后僅發送帶有指定文件的請求(作為字節Blob)來解決此問題。
如果您需要經過身份驗證的會話中的cookie,則WebDriver.magage()。getCookies()應該在這方面為您提供幫助。
編輯:我在某個可行的地方有此代碼,我看看是否可以使用您可以使用的東西。
public RosterPage UploadRosterFile(String filePath){
Face().Log("Importing Roster...");
LoginRequest login = new LoginRequest();
login.username = Prefs.EmailLogin;
login.password = Prefs.PasswordLogin;
login.rememberMe = false;
login.forward = "";
login.schoolId = "";
//Set up request data
String url = "http://www.foo.bar.com" + "/ManageRoster/UploadRoster";
String javaScript = "return $('#seasons li.selected') .attr('data-season-id');";
String seasonId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
javaScript = "return Foo.Bar.data.selectedTeamId;";
String teamId = (String)((IJavaScriptExecutor)Driver().GetBaseDriver()).ExecuteScript(javaScript);
//Send Request and parse the response into the new Driver URL
MultipartForm form = new MultipartForm(url);
form.SetField("teamId", teamId);
form.SetField("seasonId", seasonId);
form.SendFile(filePath,LoginRequest.sendLoginRequest(login));
String response = form.ResponseText.ToString();
String newURL = StaticBaseTestObjs.RemoveStringSubString("http://www.foo.bar.com" + response.Split('"')[1].Split('"')[0],"amp;");
Face().Log("Navigating to URL: "+ newURL);
Driver().GoTo(new Uri(newURL));
return this;
}
MultiPartForm在哪里: MultiPartForm
和LoginRequest / Response: LoginRequest LoginResponse
上面的代碼在C#中,但是Java中存在等效的基類,它們會執行您需要它們進行模仿的功能。
所有這些代碼中最重要的部分是MultiPartForm.SendFile方法,這是發生魔術的地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.