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/upload")
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"))
I have also tried the obvious:
file_path=os.path.abspath("relative/path/to/file") print(file_path) file_input = browser.find_element_by_css_selector('input(type="file")')) file_input.send_keys(file_path)
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?