0

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

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

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!! :)

This is what i try:

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")); 
}
Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
Jayyrus
  • 12,961
  • 41
  • 132
  • 214

1 Answers1

1

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

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.

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 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.

Petr Janeček
  • 37,768
  • 12
  • 121
  • 145
  • on that input type file, there is a javascript call "onChange". if i run your code, it will set the path in the input but don't load the method onChange, suggest? – Jayyrus May 09 '12 at 23:14
  • 1
    I was pretty sure that the call would trigger. But since this is a nicely done js, you can call it manually: `(JavascriptExecutor)d.executeScript("onChangeFileBrowse('fileField')");` – Petr Janeček May 09 '12 at 23:52
  • i'm trying in this site : http://www3.subito.it/ai/form/0 so i try ((JavascriptExecutor)d).executeScript("showSpindle();submitImage(image);"); but it returns me: Exception in thread "main" org.openqa.selenium.WebDriverException: null (WARNING: The server did not provide any stacktrace information) – Jayyrus May 10 '12 at 05:52
  • That's a different one :). Do `showSpindle(); submitImage(document.getElementById('image'));` – Petr Janeček May 10 '12 at 08:17
  • first of all i need to thank you for your answers.. the function "submitImage(var)" when is called, it creates an input type="hidden" with value="Load" and then submits the form. so it will refresh page and upload image. the code you write to me don't let the form to submit, but the method is called.. how can be wrong? – Jayyrus May 10 '12 at 11:17
  • 1
    I just found a general solution to this - I will edit it into the answer. – Petr Janeček May 10 '12 at 12:18
  • i don't know why it works now... i don't change nothing... but good! :) thanks!! – Jayyrus May 11 '12 at 09:19