可能重复:
How to upload a file from a site using Selenium's java inteface如何使用Selenium在Internet Explorer中自动上传文件?
我是新来Selenium.Can你请电话我如何在Internet Explorer中使用Selenium自动化文件上传?
可能重复:
How to upload a file from a site using Selenium's java inteface如何使用Selenium在Internet Explorer中自动上传文件?
我是新来Selenium.Can你请电话我如何在Internet Explorer中使用Selenium自动化文件上传?
这并不容易,而且出于很好的理由 - 安全性并不容易。如果您能够上传这样的内容,那么阻止某人使用相同方法上传您的详细信息的是什么?
你也给了我们没有例子有如此工作:
鉴于此范例网页:
<html>
<head>
<style type="text/css">
.fileSave { color: red; }
</style>
</head>
<label for="fileUpload">File location:
<input type="file" id="fileUpload" />
<br>
<br>
<a href="" class="fileSave">Save file</a>
</body>
</html>
我能做到这一点,在C#:
Driver = new ChromeDriver();
var fileUploadControl = Driver.FindElement(By.Id("fileUpload"));
fileUploadControl.SendKeys("C:\File.txt");
var submitLink = Driver.FindElement(By.ClassName("fileSave"));
submitLink.Click();
这一直问several times,也在一些Selenium常见问题。
硒2(的webdriver)Java示例:
// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.xpath("//input[@type='file']"));
fileInput.sendKeys("C:/path/to/file.jpg");
硒RC,见this question。
这个想法是直接将文件的绝对路径发送到您通常会点击以获取模式窗口的元素 - 即<input type='file' />
元素。
“如果你能够上传这样的内容,什么阻止某人使用相同的方法上传你的详细信息?”在一定程度上,CAPTCHA将会。 – fixxxer