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 selenium.webdriver.common.by import By

path ="/dev/chromedriver"
driver=webdriver.Chrome(path)
driver.get("https://xxxxx.my.salesforce.com/")

""" The user logon works fine, real info stripped ''''
element_uid.send_keys("x.x@x.com").
element_pw = driver.find_element_by_name("pw")
element_pw.send_keys("xxxxxx")
element_pw.send_keys(Keys.RETURN)

""" Move to the files page, this works to """
driver.get("https://xxxxx.lightning.force.com/lightning/o/ContentDocument/home

""" Need to upload the file test.csv , this does not work """


Winning solution

# Solution

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

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
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)
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.
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!
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"]"}
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.
The Upload Files button is highlighted in the elements panel.
https://imgur.com/a/pRAwuFw
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.
No worries on the late reply. I am using OSX (10:15) but could also execute the scripts on Windows 10 if needed.
Great! Could you please try out if this script works for you. You can install additional dependency by running pip[3] install pyautogui.