如何使用Selenium的Java接口从站点上载文件 [英] How to upload a file from a site using Selenium's java inteface

查看:52
本文介绍了如何使用Selenium的Java接口从站点上载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是硒开发的新手,我需要刮"此页面:

i'm new in selenium developing and i need to "scrape" this page:

https://annunci.ebay.it/pubblica-annuncio

在此页面中,使用jquery上传图像.我需要在Java中使用Selenium进行相同的操作.有人可以给我一些建议吗?谢谢!!:)

in this page there is an images upload using jquery. i need to do the same using selenium in java. can someone give me some suggests? thanks!! :)

这是我尝试的:

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")); 
}

推荐答案

如果我对您的问题理解正确,那么您想在此处上传文件,对吗?这应该可以解决问题:

If I understand your question right, you want to upload a file there, is that correct? This should do the trick:

d.findElement(By.id("fileField")).sendKeys("C:\\absolutePath\\toYour\\file.jpg");

想法是将文件的路径直接发送到通常会单击以获取模式窗口的元素-即< input type ='file'/> 元素

The idea is to directly send the path to the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element.

由于大多数文件上传输入都有一些 onchange 事件触发器,因此您应该能够触发这些事件触发器.根据 WebDriver常见问题解答,一个简单的解决方案,因此您不必手动触发所有这些事件:

Because most of those file upload inputs have some onchange event trigger, you should be able to fire those. According to WebDriver FAQ, there is a simple solution for this so you don't have to fire all those events manually:

WebDriver将焦点留在您称为"sendKeys"的元素上.仅当焦点离开该元素时才会触发"onchange"事件.作为这样,您需要移动焦点,也许可以通过点击"另一个元素.

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.

这篇关于如何使用Selenium的Java接口从站点上载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆