Feline
Feline

Reputation: 784

How to send a file to an upload button in selenium?

How can I send a file to the browser in a headless selenium session (as opposed to clicking on an "upload" button)?

For example, how do I add a file to a form like this without using the gui: enter image description here

Upvotes: 1

Views: 153

Answers (1)

Prophet
Prophet

Reputation: 33361

In most cases there is an input element with type file there, so you can directly send your file path to it.
For example if your file path on the disk is C:\\Users\\Desktop\\test.png your code will be:

file_path = "C:\\Users\\Desktop\\test.png"
upload_element = driver.find_element_by_xpath("//input[@type='file']")
upload_element.send_keys(file_path)

Upvotes: 1

Related Questions