selenium打开浏览器底层实现原理中的异常1(Unable to determine type)
代码:
结果:
PS E:\30.Study\30.自动化测试\99.零基础入门 Python Web 自动化测试\10.seleniumCodePractice> & "C:/Program Files/Python38/python.exe" "e:/30.Study/30.自动化测试/99.零基础入门 Python Web 自动化测试/10.seleniumCodePractice/202006/requests_open_browser.py"
{'value': {'stacktrace': "org.openqa.selenium.json.JsonException: Unable to determine type from: c. Last 1 characters read: c\nBuild info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'\nSystem info: host: 'DESKTOP-14VQUSG', ip: '192.168.137.1', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_251'\nDriver info: driver.version: unknown\r\n\tat org.openqa.selenium.json.JsonInput.peek(JsonInput.java:122)\r\n\tat org.openqa.selenium.json.JsonInput.expect(JsonInput.java:288)\r\n\tat org.openqa.selenium.json.JsonInput.beginObject(JsonInput.java:220)\r\n\tat org.openqa.selenium.remote.NewSessionPayload.getOss(NewSessionPayload.java:338)\r\n\tat org.openqa.selenium.remote.NewSessionPayload.<init>(NewSessionPayload.java:145)\r\n\tat org.openqa.selenium.remote.NewSessionPayload.create(NewSessionPayload.java:105)\r\n\tat org.openqa.selenium.remote.server.commandhandler.BeginSession.execute(BeginSession.java:64)\r\n\tat org.openqa.selenium.remote.server.WebDriverServlet.lambda$handle$0(WebDriverServlet.java:235)\r\n\tat java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)\r\n\tat java.util.concurrent.FutureTask.run(Unknown Source)\r\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)\r\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)\r\n\tat java.lang.Thread.run(Unknown Source)\r\n", 'stackTrace': [{'fileName': 'JsonInput.java', 'methodName': 'peek', 'className': 'org.openqa.selenium.json.JsonInput', 'lineNumber': 122}, {'fileName': 'JsonInput.java', 'methodName': 'expect', 'className': 'org.openqa.selenium.json.JsonInput', 'lineNumber': 288}, {'fileName': 'JsonInput.java', 'methodName': 'beginObject', 'className': 'org.openqa.selenium.json.JsonInput', 'lineNumber': 220}, {'fileName': 'NewSessionPayload.java', 'methodName': 'getOss', 'className': 'org.openqa.selenium.remote.NewSessionPayload', 'lineNumber': 338}, {'fileName': 'NewSessionPayload.java', 'methodName': '<init>', 'className': 'org.openqa.selenium.remote.NewSessionPayload', 'lineNumber': 145}, {'fileName': 'NewSessionPayload.java', 'methodName': 'create', 'className': 'org.openqa.selenium.remote.NewSessionPayload', 'lineNumber': 105}, {'fileName': 'BeginSession.java', 'methodName': 'execute', 'className': 'org.openqa.selenium.remote.server.commandhandler.BeginSession', 'lineNumber': 64}, {'fileName': 'WebDriverServlet.java', 'methodName': 'lambda$handle$0', 'className':
'org.openqa.selenium.remote.server.WebDriverServlet', 'lineNumber': 235}, {'fileName': None, 'methodName': 'call', 'className': 'java.util.concurrent.Executors$RunnableAdapter', 'lineNumber': -1}, {'fileName': None, 'methodName': 'run', 'className': 'java.util.concurrent.FutureTask', 'lineNumber': -1}, {'fileName': None, 'methodName': 'runWorker', 'className': 'java.util.concurrent.ThreadPoolExecutor', 'lineNumber': -1}, {'fileName': None, 'methodName': 'run', 'className': 'java.util.concurrent.ThreadPoolExecutor$Worker', 'lineNumber': -1}, {'fileName': None, 'methodName': 'run', 'className': 'java.lang.Thread', 'lineNumber': -1}], 'message': "Unable to determine type from: c. Last 1 characters read: c\nBuild info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'\nSystem info: host: 'DESKTOP-14VQUSG', ip: '192.168.137.1', os.name: 'Windows 10', os.arch: 'x86', os.version: '10.0', java.version: '1.8.0_251'\nDriver info: driver.version: unknown", 'error': 'unknown error'}, 'status': 13}
PS E:\30.Study\30.自动化测试\99.零基础入门 Python Web 自动化测试\10.seleniumCodePractice>
原因分析:
从【 Unable to determine type 】可知,requests.post(url,data)转成json-encoded的data时失败了(*1)。之所以失败,因为对于data来说它是一个对象(Serialize obj
),要使一个对象能够使用json(),那就必须使这个对象是JSON formatted格式的字符串,所以需要使用json.dumps方法(*2)
*1.requests.post(url,data).json()的json():Returns the json-encoded content of a response, if any.(从VSCode看到的json()的解释)
*2.json.dumps()解释:
def dumps(obj: Any, skipkeys: bool=..., ensure_ascii: bool=..., check_circular: bool=..., allow_nan: bool=..., cls: Optional[Type[JSONEncoder]]=..., indent: Union[None, int, str]=..., separators: Optional[Tuple[str, str]]=..., default: Optional[Callable[[Any], Any]]=..., sort_keys: bool=..., **kwds: Any)
Serialize obj to a JSON formatted str.
If skipkeys is true then dict keys that are not basic types (str, int, float, bool, None) will be skipped instead of raising a TypeError.
If ensure_ascii is false, then the return value can contain non-ASCII characters if they appear in strings contained in obj. Otherwise, all such characters are escaped in JSON strings.
If check_circular is false, then the circular reference check for container types will be skipped and a circular reference will result in an OverflowError (or worse).
If allow_nan is false, then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN, Infinity, -Infinity).
If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.
If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if *indent* is None and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.
default(obj) is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.
If *sort_keys* is true (default: False), then the output of dictionaries will be sorted by key.
To use a custom JSONEncoder subclass (e.g. one that overrides the .default() method to serialize additional types), specify it with the cls kwarg; otherwise JSONEncoder is used.
对应方法:
==》
posted on 2020-06-19 00:03 shiberhou 阅读(1116) 评论(0) 编辑 收藏 举报