How to upload a file from a site using Selenium's java inteface

前端 未结 1 1256
慢半拍i
慢半拍i 2021-01-17 00:51

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 imag

相关标签:
1条回答
  • 2021-01-17 01:09

    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.

    0 讨论(0)
提交回复
热议问题