Python 使用selenium webdriver上载文件时引发WebDriverException

Python 使用selenium webdriver上载文件时引发WebDriverException,python,selenium,upload,webdriver,Python,Selenium,Upload,Webdriver,我想使用以下python代码上载文件: driver.find_element_by_id("fileFieldName-file").send_keys("D:\\manual.pdf") 该代码在Firefox中运行良好,但在IE和Chrome中失败。例外情况如下: WebDriverException:消息:“{状态:404,会话ID:,值:未找到命令:POST/session/e56793e2-79f9-4bb9-820e-91090ccee083/file}”我自己找到了答案。 这是

我想使用以下python代码上载文件:

driver.find_element_by_id("fileFieldName-file").send_keys("D:\\manual.pdf")
该代码在Firefox中运行良好,但在IE和Chrome中失败。例外情况如下:


WebDriverException:消息:“{状态:404,会话ID:,值:未找到命令:POST/session/e56793e2-79f9-4bb9-820e-91090ccee083/file}”

我自己找到了答案。 这是硒的一个缺陷。解决方案如下: 1.在C:\Python27\Lib\site packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote中找到文件webelement.py 2.在webelement.py中找到函数_upload 3.更改函数上传的代码,在异常处理部分添加条件

def _upload(self, filename):
    fp = StringIO()
    zipped = zipfile.ZipFile(fp, 'w', zipfile.ZIP_DEFLATED)
    zipped.write(filename)
    zipped.close()
    try:
        return self._execute(Command.UPLOAD_FILE, 
                        {'file': base64.encodestring(fp.getvalue())})['value']
    except WebDriverException as e:
        if "Unrecognized command: POST" in e.__str__():
            return filename
        elif "Command not found: POST" in e.__str__():
            return filename
        else:
            raise e
强文本