我是 Selenium 开发新手,我需要“抓取”此页面:
https://annunci.ebay.it/pubblica-annuncio
在此页面中有一个使用 jquery 上传的图像。我需要在java中使用selenium做同样的事情。有人可以给我一些建议吗?谢谢!! :)
这就是我尝试的:
WebDriver d = new FirefoxDriver();
d.get("https://annunci.ebay.it/pubblica-annuncio");
List<WebElement> elements = d.findElements(By.tagName("input"));
for(WebElement e : elements){
System.out.println(e.getAttribute("name"));
}
最佳答案
如果我理解你的问题是正确的,你想在那里上传一个文件,对吗?这应该可以解决问题:
d.findElement(By.id("fileField")).sendKeys("C:\\absolutePath\\toYour\\file.jpg");
这个想法是将文件的路径直接发送到您通常单击以获取模式窗口的元素 - 即 <input type='file' />
元素。
因为大多数文件上传输入都有一些 onchange
事件触发器,您应该能够触发它们。根据WebDriver FAQ ,有一个简单的解决方案,因此您不必手动触发所有这些事件:
WebDriver leaves the focus in the element you called "sendKeys" on. The "onchange" event will only fire when focus leaves that element. As such, you need to move the focus, perhaps using a "click" on another element.
关于java - 如何使用 Selenium java 接口(interface)从网站上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10522372/