Python selenium webdrive 无法上传文件并出现“未知命令”异常

问题描述 投票:0回答:3

我尝试在 Facebook Messager 上上传图像/文件,其示例 URL 为 https://www.facebook.com/messages/t/471646182511990(如果您想尝试,您需要在 Chrome 上打开 Facebook Messager浏览器并获取有效的 URL)。但它因“未知命令”异常而失败。

我搜索但找不到任何合适的答案。线程“https://stackoverflow.com/questions/63272928/selenium-webdriver-python-cant-upload-file-send-keys-is-throws-elementnotinte”很接近,但不是同一个问题。

版本:

  1. Python 3.9
  2. 硒:4.1.0

代码:

def sendMedia(mediaFile):
    filePath="file.jpg")
    try:
        fileInputBox = driver.find_element_by_xpath("//input[@type='file']")
    except
        return False
    fileInputBox.send_keys(filePath)
    sleep(randint(1,2))
    fileInputBox.send_keys(Keys.ENTER)
    sleep(randint(5, 6))
    return True

问题:这一行“fileInputBox.send_keys(filePath)”返回以下错误:

回溯(最近一次调用最后一次): 文件“D:\Workspace\Property\Facebook env\lib\site-packages\selenium\webdriver emote\webelement.py”,第 785 行,在 _upload 中 提高e

文件“D:\Workspace\Property\Facebook env\lib\site-packages\selenium\webdriver emote\webelement.py”,第 776 行,在 _upload 中 return self._execute(Command.UPLOAD_FILE, {'file': content})['value']

文件“D:\Workspace\Property\Facebook env\lib\site-packages\selenium\webdriver emote\webelement.py”,第 710 行,在 _execute 中 返回 self._parent.execute(命令,参数)

文件“D:\Workspace\Property\Facebook env\lib\site-packages\selenium\webdriver emote\webdriver.py”,第 424 行,执行中 self.error_handler.check_response(响应)

文件“D:\Workspace\Property\Facebook env\lib\site-packages\selenium\webdriver emote rrorhandler.py”,第 247 行,在 check_response 中 引发异常类(消息,屏幕,堆栈跟踪)

selenium.common.exceptions.WebDriverException:消息:未知命令:未知命令:会话/83896857b2899e4a7c53c4a8dca78976/se/file

堆栈跟踪:

回溯:

Ordinal0 [0x00C16903+2517251]

Ordinal0 [0x00BAF8E1+2095329]

Ordinal0 [0x00AB2848+1058888]

Ordinal0 [0x00AF429E+1327774]

Ordinal0 [0x00AF4089+1327241]

Ordinal0 [0x00A950AF+938159]

Ordinal0 [0x00A955C6+939462]

Ordinal0 [0x00A958F1+940273]

GetHandleVerifier [0x00DA5904+1577972]

GetHandleVerifier [0x00E50B97+2279047]

GetHandleVerifier [0x00CA6D09+534521]

GetHandleVerifier [0x00CA5DB9+530601]

Ordinal0 [0x00BB4FF9+2117625]

Ordinal0 [0x00A94E40+937536]

Ordinal0 [0x00A94848+936008]

GetHandleVerifier [0x00E78B5C+2442828]

BaseThreadInitThunk [0x7732FA29+25]

RtlGetAppContainerNamedObjectPath [0x775C7A9E+286]

RtlGetAppContainerNamedObjectPath [0x775C7A6E+238]
python selenium exception
3个回答
1
投票

我对 Selenium 4.1.0 和 Python 3.9 也有同样的问题。 我做了一些解决方法,但我不喜欢它。

在remote_connection.py 文件中的selenium site-package 中,我已将第218行更改为:

Command.UPLOAD_FILE: ('POST', "/session/$sessionId/se/file"),
到:
Command.UPLOAD_FILE: ('POST', "/session/$sessionId/file"),

现在它正在工作,但正如我所说,我想它不应该是这样的。


0
投票

马克的答案对我有用。

为了保持包代码干净,我使用了如下的解决方法:

from selenium.webdriver import Remote
from selenium.webdriver.remote import remote_connection
from selenium.webdriver.remote.command import Command

def do_something(url: str, session_id: str)

    rmt_con = remote_connection.RemoteConnection(url)
    rmt_con._commands.update({
        Command.UPLOAD_FILE: ("POST", "/session/$sessionId/file")
    })
    driver = Remote(command_executor=rmt_con, desired_capabilities={})
    driver.session_id = session_id
    
    # Do amazing things afterwards
    driver.get('')


0
投票

有人可以帮助我吗?我面临着同样的问题,但我正在使用 Selenium java。附加 StackOverflow 问题供参考:Selenium Java UnsupportedCommandException 未知命令:文件上传期间的 session/9845d50a442f6c23dc498210a0d253d7/se/file

© www.soinside.com 2019 - 2024. All rights reserved.