1

重要提示:我正在使用 selenium==4.0.0b3

在使用 selenium-webdriver 上传现有文件的过程中,我进行了测试以发现该文件是否确实存在,以证实我实际上正在上传某些内容并与之保持一致,我的测试代码:

print("Is filepath", os.path.isfile(local_photo_path))
file_inputs[0].send_keys(os.path.abspath(local_photo_path))

    

文件路径给了我一个 True 值,但出现以下错误:

Is filepath True
ERROR [_CatchWebDriverError][204] invalid argument: File not found : /tmp/69f9dddf-6f69-490e-9ed3-d9a379e151af.jpg
  (Session info: chrome=89.0.4389.90)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found : /tmp/69f9dddf-6f69-490e-9ed3-d9a379e151af.jpg
  (Session info: chrome=89.0.4389.90)
Stacktrace:
#0 0x56282084f2b9 <unknown>

阅读 Selenium 文档,看来我做得对,这是实际的 selenium 函数:

def send_keys(self, *value) -> None:
    """Simulates typing into the element.

    :Args:
        - value - A string for typing, or setting form fields.  For setting
          file inputs, this could be a local file path.

    Use this to send simple key events or to fill out form fields::

        form_textfield = driver.find_element(By.NAME, 'username')
        form_textfield.send_keys("admin")

    This can also be used to set file inputs.

    ::

        file_input = driver.find_element(By.NAME, 'profilePic')
        file_input.send_keys("path/to/profilepic.gif")
        # 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.
        # file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))

    """
    # transfer file to another machine only if remote driver is used
    # the same behaviour as for java binding
    print('This is send keys')
    if self.parent._is_remote:
        local_files = list(map(lambda keys_to_send:
                               self.parent.file_detector.is_local_file(str(keys_to_send)),
                               ''.join(map(str, value)).split('\n')))
        if None not in local_files:
            remote_files = []
            for file in local_files:
                remote_files.append(self._upload(file))
            value = '\n'.join(remote_files)

    self._execute(Command.SEND_KEYS_TO_ELEMENT,
                  {'text': "".join(keys_to_typing(value)),
                   'value': keys_to_typing(value)})
4

0 回答 0