How to upload file using Selenium WebDriver in Java

后端 未结 6 1060
逝去的感伤
逝去的感伤 2020-11-22 10:34

Can anyone let me know how to upload a file using Selenium by Java code?

When I click on button in the application it gets open in the new window what I can use to s

6条回答
  •  盖世英雄少女心
    2020-11-22 11:13

    First make sure that the input element is visible

    As stated by Mark Collin in the discussion here:

    Don't click on the browse button, it will trigger an OS level dialogue box and effectively stop your test dead.

    Instead you can use:

    driver.findElement(By.id("myUploadElement")).sendKeys("");

    myUploadElement is the id of that element (button in this case) and in sendKeys you have to specify the absolute path of the content you want to upload (Image,video etc). Selenium will do the rest for you.

    Keep in mind that the upload will work only If the element you send a file should be in the form

提交回复
热议问题