我遇到浏览按钮问题并切换到文件对话框。我不能使用我的文件路径控制,只是发送我的字符串文件路径和文件本身,因为它是只读的,实际上一些控制后面是我的输入文件路径。
这是我的代码
driver.FindElement(By.Id("browseButton")).Click();
driver.SwitchTo().ActiveElement().SendKeys(filepath);
上面的代码填满了我对文件路径的控制,因为我可以在UI上看到它。但我的打开文件对话框仍然打开,我不知道如何关闭它并提交我的上传。
答案 0 :(得分:2)
至少可以说,在Selenium中上传文件会很麻烦。真正的问题来自于它不支持文件上传和下载等对话框。
我在回答另一个问题时回答这个问题,所以我将在这里复制/粘贴我的答案。代码示例实际上应该与您的情况相关,因为您使用的是C#:
Selenium Webdriver并不真正支持这一点。与非浏览器窗口交互(例如本机文件上载对话框和基本身份验证对话框)一直是WebDriver讨论板上讨论的主题,但在这个主题上几乎没有任何进展。
过去,我已经能够通过使用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方法,这就是魔术发生的地方。
答案 1 :(得分:1)
执行此操作的众多方法之一是删除disable属性,然后使用典型的selenium SendKeys()
来完成
public void test(string path)
{
By byId = By.Id("removeAttribute");
const string removeAttribute = @"document.getElementById('browseButton').removeAttribute('disabled');";
((IJavaScriptExecutor)Driver).ExecuteScript(removeAttribute);
driver.FindElement(byId).Clear();
driver.FindElement(byId).SendKeys(path);
}
答案 2 :(得分:1)
You can use this Auto IT Script to Handle File Upload Option.
Auto IT Script for File Upload:
AutoItSetOption("WinTitleMatchMode","2") ; set the select mode to
Do
Sleep ("1000")
until WinExists("File Upload")
WinWait("File Upload")
WinActivate("File Upload")
ControlFocus("File Upload","","Edit1")
Sleep(2000)
ControlSetText("File Upload" , "", "Edit1", $CmdLineRaw)
Sleep(2000)
ControlClick("File Upload" , "","Button1");
Build and Compile the above code and place the EXE in a path and call it when u need it.
Call this Once you click in the Browse Button.
Process p = System.Diagnostics.Process.Start(txt_Browse.Text + "\\File Upload", DocFileName);
p.WaitForExit();