使用selenium webdriver上传文件时抛出WebDriverException

时间:2012-06-14 02:18:47

标签: python selenium upload webdriver

我想使用以下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" }'

1 个答案:

答案 0 :(得分:0)

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

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

强文