Reputation: 3717
I have encountered a problem uploading a file with selenium, and I'd like to understand why this occurs. I noticed the following:
file_input = browser.find_element_by_css_selector('input[type="file"]'))
file_input.send_keys("/absolute/path/to/file.jpeg")
That works fine on my machine. However, I need the tests to run in CI, so the path I provide must be dynamic. That's where I've encountered a problem; I am trying to use os.path.abspath()
to get the absolute path on any system, and it is indeed working to get the absolute path. Printing shows that locally, it gets the exact same path that I use above, but when I do the following, it does not work. (By this I mean the no error occurs, but it doesn't seem to even try to upload the file).
file_input = browser.find_element_by_css_selector('input[type="file"]'))
file_input.send_keys(os.path.abspath("relative/path/to/file.jpeg"))
I have also tried the obvious:
file_path=os.path.abspath("relative/path/to/file.jpeg")
print(file_path)
file_input = browser.find_element_by_css_selector('input[type="file"]'))
file_input.send_keys(file_path)
Logging file_path
shows the correct absolute filepath. This also doesn't upload the file, failing silently without an error.
I'm confident I can find a workaround (another way to specify the right absolute path dynamically), but I'd really like to understand: why doesn't this work? I know there is some sort of "magic" done by selenium to upload file, and I haven't seen that code at all, so maybe there is something there? Also python is not my strongest language, so perhaps I'm doing something wrong from that perspective.
Can anyone help me understand why this happens?
Upvotes: 2
Views: 1376
Reputation: 15629
I believe that your problem is related to the path variable in send_keys().
In my testing I was able to use os.path.abspath("relative/path/to/file.jpeg")
, but this file does not exist. I would recommending trying something like this, which verifies that the file exist at the path specified.
import os
def get_local_path():
current_dir = os.path.dirname(os.path.realpath(__file__))
return current_dir
def get_file_path(filename):
current_dir = get_local_path()
current_file_path = os.path.join(current_dir, filename)
if os.path.exists(current_file_path):
return current_file_path
else:
return ('file path does not exist')
# I don't know the exact local of your file, so you might
# have to do some testing.
file_to_upload = get_file_path('file.jpeg')
file_input = browser.find_element_by_css_selector('input[type="file"]'))
file_input.send_keys(file_to_upload)
I also looked through the source code of selenium
and I don't see that send_keys()
will throw an error when something is inputted incorrect. The browser might display a message when something is entered incorrectly. When that happens you usually have to extract that error message using selenium.
. Have you verified that no error messages are being thrown in the browser?
Have you considered validating the send_key()
input? Here is a line of code that would help with this.
validate_file = file_input.get_attribute('value')
# add code to check to see if the value matches what you sent
Upvotes: 1