【问题标题】:throw WebDriverException when using selenium webdriver to upload file使用 selenium webdriver 上传文件时抛出 WebDriverException
【发布时间】:2012-06-14 02:18:47
【问题描述】:

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

driver.find_element_by_id("fileFieldName-file").send_keys("D:\\manual.pdf")

该代码在 Firefox 中运行良好,但在 IE 和 Chrome 中失败。例外情况如下:

WebDriverException: Message: '{ "status" : 404, "sessionId" : "<no session>", "value" : "Command not found: POST /session/e56793e2-79f9-4bb9-820e-91090ccee083/file" }'

【问题讨论】:

    标签: python selenium upload webdriver


    【解决方案1】:

    我自己找到答案。 http://code.google.com/p/selenium/issues/detail?id=3736 这是selenium的一个bug。解决方法如下: 1.在C:\Python27\Lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote中找到文件“webelement.py” 2.在“webelement.py”中找到函数“_upload” 3.修改“_upload”函数的代码,在异常处理部分添加条件。

    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
    

    强文本

    【讨论】:

      猜你喜欢
      • 2020-11-26
      • 1970-01-01
      • 2017-10-02
      • 2013-09-20
      • 2015-03-22
      • 2015-12-23
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多