Tăng Đức Thịnh
Tăng Đức Thịnh

Reputation: 13

upload file by Chrome File Upload dialog using selenium webdriver in headless mode in python

I'm trying to use the selenium chrome driver in headless mode to upload a file to a web, but the web doesn't support the input tags to use send_key, it opens a Chrome File Upload dialog. I try to use pyautogui to handle keyboard and type the file path in my computer to the dialog, but it just works without headless mode.

Is there any idea to solve this upload problem?

Upvotes: 0

Views: 2431

Answers (1)

CEH
CEH

Reputation: 5909

You might want to try running some Javascript to reveal the input element. I've personally run tests in headless mode that perform this exact function with success.

# Fetch file input element
fileInput = driver.find_element_by_xpath("//input[@type='file']")

#  Execute Javascript to reveal the element
driver.execute_script("arguments[0].style.display = 'block';", fileInputElement)

# Send keys to file input
fileInput.send_keys("Path/To/File/To/Upload")

Once you do this, you can send_keys to the hidden element.

Upvotes: 2

Related Questions