0

次の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" }'

4

1 に答える 1

0

私は自分で答えを見つけます。 http://code.google.com/p/selenium/issues/detail?id=3736 これはSeleniumのバグです。解決策は次のとおりです。1。C:\ Python27\Libでファイル「webelement.py」を見つけます。 \ site-packages \ selenium-2.21.2-py2.7.egg \ selenium \ webdriver \ remote2.「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

強いテキスト

于 2012-06-18T08:55:17.747 に答える