我想附加一个文件进行上传。测试的一部分我正在尝试以下但它无法正常工作
1) Enter some details into the input fields
2) Click Browse on the Web site
3) Enter a file path in the windows popup that appears
4) Click Open on the windows popup
5) Click Apply on the Web site
Driver.FindElement(By.Id("Name")).SendKeys(name);
Driver.FindElement(By.Id("EmailAddress")).SendKeys(email);
Driver.FindElement(By.Id("TelephoneNumber")).SendKeys(telephone.ToString());
Driver.FindElement(By.Name("file")).Click();
Driver.SwitchTo().ActiveElement().SendKeys("/home/likewise-open/GLOBAL/123/Documents/filename.txt");
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
Driver.FindElement(By.Id("convertButton"));
答案 0 :(得分:1)
由于WebDriver只能处理基于Web的交互,而不能处理基于窗口的交互, 您需要AutoIT实用程序才能上传或下载并将其集成到Selenium脚本中。 AutoIT是一款免费工具。
答案 1 :(得分:1)
试试这段代码
Driver.FindElement(By.Name("yourUploadFileTextBox")).SendKeys("/home/likewise-open/GLOBAL/123/Documents/filename.txt");
Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
Driver.FindElement(By.Id("convertButton"));
它会起作用。无需单击“浏览”按钮。只需在textBox出现的textBox上发送。