• selenium打开浏览器底层实现原理中的异常1(Unable to determine type)


    代码:

    #coding=utf-8
    import requests
    import json
    url='http://127.0.0.1:4444/wd/hub/session'
    data = {
        'desiredCapabilities':{
            'browserName':'MicrosoftEdge'
        }
    }
    print(requests.post(url,data).json())

    结果:

    PS E:30.Study30.自动化测试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 Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53' System 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' Driver info: driver.version: unknown at org.openqa.selenium.json.JsonInput.peek(JsonInput.java:122) at org.openqa.selenium.json.JsonInput.expect(JsonInput.java:288) at org.openqa.selenium.json.JsonInput.beginObject(JsonInput.java:220) at org.openqa.selenium.remote.NewSessionPayload.getOss(NewSessionPayload.java:338) at org.openqa.selenium.remote.NewSessionPayload.<init>(NewSessionPayload.java:145) at org.openqa.selenium.remote.NewSessionPayload.create(NewSessionPayload.java:105) at org.openqa.selenium.remote.server.commandhandler.BeginSession.execute(BeginSession.java:64) at org.openqa.selenium.remote.server.WebDriverServlet.lambda$handle$0(WebDriverServlet.java:235) at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) ", '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 Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53' System 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' Driver info: driver.version: unknown", 'error': 'unknown error'}, 'status': 13}
    PS E:30.Study30.自动化测试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.

    对应方法:

    data = {
        'desiredCapabilities':{
            'browserName':'MicrosoftEdge'
        }

    ==》

    data = json.dumps({
        'desiredCapabilities':{
            'browserName':'MicrosoftEdge'
        }
    })
  • 相关阅读:
    bzoj 2038 [2009国家集训队]小Z的袜子(hose)
    【NOIP2014模拟11.1B组】吴传之火烧连营
    【NOIP2014模拟11.1B组】蜀传之单刀赴会
    phpmystudy:mysql启动失败
    英文漏洞报告解读(一)——PHP 5.4.x < 5.4.32 Multiple Vulnerabilities
    brupsuit Compare 模块及其应用场景
    Android实现Banner界面广告图片循环轮播(包括实现手动滑动循环)
    android ViewPager实现的轮播图广告自定义视图,网络获取图片和数据
    Android首页轮播图直接拿来用
    java 调用webservice的各种方法总结
  • 原文地址:https://www.cnblogs.com/hadas/p/13160867.html
Copyright © 2020-2023  润新知