Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Red herring for Cygwin users in send_keys docstring #15272

Closed
wemily opened this issue Feb 11, 2025 · 7 comments
Closed

Red herring for Cygwin users in send_keys docstring #15272

wemily opened this issue Feb 11, 2025 · 7 comments

Comments

@wemily
Copy link

wemily commented Feb 11, 2025

https://www.selenium.dev/selenium/docs/api/py/_modules/selenium/webdriver/remote/webelement.html#WebElement.send_keys

Generally it's better to wrap the file path in one of the methods in os.path to return the actual path to support cross OS testing.

That advice is misleading to Cygwin users getting InvalidArgumentException: File not found from send_keys with Geckodriver, which needs the Windows-style absolute path. On Cygwin, os.path returns Posix-style paths.

>>> os.path.exists("example.jpg")
True
>>> os.path.abspath("example.jpg")
'/home/user1/project2/example.jpg'

>>> file_input = self.driver.find_element(By.CSS_SELECTOR, "input[type='file']")
>>> file_input.send_keys(os.path.abspath("example.jpg"))
Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 232, in send_keys
    Command.SEND_KEYS_TO_ELEMENT, {"text": "".join(keys_to_typing(value)), "value": keys_to_typing(value)}
  File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
  File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 346, in execute
    self.error_handler.check_response(response)
  File "/usr/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: File not found: /home/user1/project2/example.jpg
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:187:5
InvalidArgumentError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:378:5
interaction.uploadFiles@chrome://remote/content/marionette/interaction.sys.mjs:543:13

>>> path_that_works = ("c:/cygwin" + os.path.abspath("example.jpg")).replace("/", "\\")
>>> file_input.send_keys(path_that_works)
>>>

Also, a syntax error or something is preventing the "Examples" from being formatted as code here:
https://www.selenium.dev/selenium/docs/api/py/webdriver_remote/selenium.webdriver.remote.webelement.html#examples

@diemol
Copy link
Member

diemol commented Feb 11, 2025

Would you like to help us improve the documentation? https://github.com/SeleniumHQ/seleniumhq.github.io

@Delta456
Copy link
Member

We would need to update the code docs as well as the documentation website.

@wemily
Copy link
Author

wemily commented Feb 11, 2025

I'm not sure what the documentation should say in this situation. Since WebDriver is just an interface, it would be weird for the documentation to contain a lot of implementation-specific detail.

I also don't know whether this issue is specific to Geckodriver. Would Chromedriver or another driver handle translating the path? I just don't know, since I have only used Geckodriver, and only for one very specific use case.

My sample code using String.replace("/", "\\") was just to illustrate the difference between the path that Python provides, and the path that Geckodriver needed. That's definitely not the right way to translate any arbitrary path, and I don't think it should go directly into the documentation.

@VietND96
Copy link
Member

Instead of String.replace(), using os.path.normpath(path_that_works). Does it return path with \ as expected?

@wemily
Copy link
Author

wemily commented Feb 12, 2025

I don't think anything along that line would result in backslashes. Prepending the drive letter (like c: but it could be a different letter) is also crucial.

>>> import os

>>> os.path.normpath("example.jpg")
'example.jpg'

>>> os.path.normpath(os.path.abspath("example.jpg"))
'/home/user1/project2/example.jpg'

>>> os.path.realpath("example.jpg")
'/home/user1/project2/example.jpg'

>>> os.path.sep
'/'

>>> os.sep
'/'

>>> os.name
'posix'

>>> import sys
>>> sys.platform
'cygwin'

This is how I solved it for myself, but that's using a Python library that isn't standard.
https://stackoverflow.com/a/79428932/5848970

One of the other answers there says that the translation really is nontrivial.

@VietND96
Copy link
Member

Oh, so I think it is up to the client's implementation. From the documentation, we can specify the detail

@Delta456
Copy link
Member

Closing this issue as we have specified the detail in #15275

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants