微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在上传现有文件期间:selenium.common.exceptions.InvalidArgumentException

如何解决在上传现有文件期间:selenium.common.exceptions.InvalidArgumentException

重要提示:我正在使用 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)})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?