java - 如何使用 Selenium java 接口(interface)从网站上传文件

标签 java selenium webdriver web-scraping

我是 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/

相关文章:

java - 优化 Java 正则表达式

testing - 你在测试 Chrome 吗?

java.net.URISyntaxException : Expected authority at index 7: http://

java - 如何使用 java Selenium WebDriver 下载文件?

java - Find Bugs - 不查找错误

java - 创建一个返回单例的工厂

java - FixedThreadPool 和 ThreadPoolTask​​Executor 有什么区别?

node.js - Selenium Node + Sauce 实验室测试 : Cannot Finish a Task

java - FluentWait 类型不是通用的;无法通过 Selenium 和 Java 使用 FluentWait 类的参数 <WebDriver> 错误进行参数化

java - Selenium Webdriver 元素识别