Python Selenium Salesforce file upload
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

How upload files to Salesforce "owned by me' files without going through the file selector dialog? The example logs on but fails to send the "test.csv" file.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from import By 

path ="/dev/chromedriver" 

""" The user logon works fine, real info stripped ''''
element_uid = driver.find_element_by_name("username")
element_pw = driver.find_element_by_name("pw")

""" Move to the files page, this works to """

 """ Need to upload the file test.csv , this does not work """ 
 driver.find_element(by=By.ID, value="uploadFile").send_keys("test.csv")

Crowdsource coding tasks.

1 Solution


Hi! This can be due to a number of reasons, but here is what I think:

1) Your file upload element is probably hidden. Make it visible.
driver.execute_script("document.querySelector('#uploadFile').style.visibility = 'visible'")
driver.execute_script("document.querySelector('#uploadFile').style.display = 'block'")

2) The file path needs to be absolute, not relative.
driver.find_element(by=By.ID, value="uploadFile").send_keys("/absolute/path/test.csv")

Let me know if this worked for you.

I get the following error after the "driver.execute_script(..." statements. selenium.common.exceptions.JavascriptException: Message: javascript error: Cannot read property 'style' of null
broadreach 2 years ago
Hi @broadreach! Have you replaced cssSelector with a real CSS selector to get that element? If you know the ID of the element, instead of cssSelector, you put #your-element-ID. Also, have you tried the second method? If you are on Windows then replace / with your drive (eg. C:\path\to\your\file.csv)
VladimirMikulic 2 years ago
You can also avoid JS completely by doing this: driver.find_element_by_id('uploadFile').send_keys('/path/to/image/file') Please note, if the element is hidden then JS is needed to make it visible.
VladimirMikulic 2 years ago
Thanks, Im not sure what to look for. A inspect the upload button shows: Upload Files looking at source code, the only place "Upload Files" shows is in a large JSON string: "UploadFiles":"Upload Files:" What should I be looking for? Thanks!
broadreach 2 years ago
Thanks, Im not sure what to look for. A inspect the upload button shows: Upload Files looking at source code, the only place "Upload Files" shows is in a large JSON string: "UploadFiles":"Upload Files:" What should I be looking for? Thanks! Running your last example: driver.findelementbyid('uploadFiles').sendkeys('/path/to/image/file') results in. selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="UploadFiles"]"}
broadreach 2 years ago
No worries, we'll take it to step by step. I guess we had a misunderstanding in the previous comment. Could you open your browser developer tools (elements tab, not JS) and select the file input element like this? It would also be helpful if you could screenshot it and upload the screenshot so I can tell you the exact command that is needed for it to work.
VladimirMikulic 2 years ago
The Upload Files button is highlighted in the elements panel.
broadreach 2 years ago
Hi @broadreach. Sorry for a late reply. I've investigated this and it seems to be a very delicate problem. send_keys("file") method works only on the input element, but Salesforce uses JavaScript to receive and upload the file. Selenium can't interact with OS windows, thus Selenium alone won't be enough. Would you mind telling me on which OS you are running the script? Thanks.
VladimirMikulic 2 years ago
No worries on the late reply. I am using OSX (10:15) but could also execute the scripts on Windows 10 if needed.
broadreach 2 years ago
Great! Could you please try out if this script works for you. You can install additional dependency by running pip[3] install pyautogui.
VladimirMikulic 2 years ago
Beautiful! Its works great, but I end up with the a open dialog, what is the best way to close it? (
broadreach 2 years ago
I am pleased to hear that. Here is updated script.
VladimirMikulic 2 years ago
Perfect, this was far more complex than I originally thought, great work!
broadreach 2 years ago
Thank you for such a generous tip. I appreciate that very much!
VladimirMikulic 2 years ago
np, you put a lot of work into this and you are extremely talented.
broadreach 2 years ago
Thank you, I really appreciate your taking the time to express that. Feel free to contact me anytime.
VladimirMikulic 2 years ago