美文网首页
selenium 分布式执行补充--填坑1

selenium 分布式执行补充--填坑1

作者: 足__迹 | 来源:发表于2019-06-22 18:18 被阅读0次

    启动主节点hub 和子节点node,编辑脚本执行,
    脚本如下:

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    
    driver = webdriver.Remote(
        command_executor="http://192.168.0.102:5555/wd/hub",
        desired_capabilities= {
            "browserName" : "chrome",
            "version" : '75.0.3770.100',
            "video": "true",
            "platform": "MAC",
            'javascriptEnable':True
        }
    
    )
    driver.get('http://www.baidu.com')
    time.sleep(5)
    assert '百度' in driver.title
    driver.quit()
    
    
    

    报错如下

    selenium.common.exceptions.WebDriverException: Message: First match w3c capabilities is zero length
    Stacktrace:
        at org.openqa.selenium.remote.NewSessionPayload.validate (NewSessionPayload.java:172)
        at org.openqa.selenium.remote.NewSessionPayload.<init> (NewSessionPayload.java:154)
        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 (Executors.java:511)
        at java.util.concurrent.FutureTask.run (FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:624)
        at java.lang.Thread.run (Thread.java:748)
    

    发现问题是建立会话是报错了打断点看下

    image.png
    image.png

    问题出现了,发现参数并没有放在字典中,不符合格式要求
    修改如下:

    #分布式执行(放在开头,python调用函数从上往下)
    添加一个判断是否符合desired_capabilities格式的函数
    _W3C_CAPABILITY_NAMES = frozenset([
        'acceptInsecureCerts',
        'browserName',
        'browserVersion',
        'platformName',
        'pageLoadStrategy',
        'proxy',
        'setWindowRect',
        'timeouts',
        'unhandledPromptBehavior',
    ])
    
    _OSS_W3C_CONVERSION = {
        'acceptSslCerts': 'acceptInsecureCerts',
        'version': 'browserVersion',
        'platform': 'platformName'
    }
    
    
    def _make_w3c_caps(caps):
        """Makes a W3C alwaysMatch capabilities object.
    
        Filters out capability names that are not in the W3C spec. Spec-compliant
        drivers will reject requests containing unknown capability names.
    
        Moves the Firefox profile, if present, from the old location to the new Firefox
        options object.
    
        :Args:
         - caps - A dictionary of capabilities requested by the caller.
        """
        caps = copy.deepcopy(caps)
        profile = caps.get('firefox_profile')
        always_match = {}
        if caps.get('proxy') and caps['proxy'].get('proxyType'):
            caps['proxy']['proxyType'] = caps['proxy']['proxyType'].lower()
        for k, v in caps.items():
            if v and k in _OSS_W3C_CONVERSION:
                always_match[_OSS_W3C_CONVERSION[k]] = v.lower() if k == 'platform' else v
            if k in _W3C_CAPABILITY_NAMES or ':' in k:
                always_match[k] = v
        if profile:
            moz_opts = always_match.get('moz:firefoxOptions', {})
            # If it's already present, assume the caller did that intentionally.
            if 'profile' not in moz_opts:
                # Don't mutate the original capabilities.
                new_opts = copy.deepcopy(moz_opts)
                new_opts['profile'] = profile
                always_match['moz:firefoxOptions'] = new_opts
        return {"firstMatch": [{}], "alwaysMatch": always_match}
    

    将一下模块修改

    image.png

    修改后

                if "moz:firefoxOptions" in capabilities:
                    capabilities["moz:firefoxOptions"]["profile"] = browser_profile.encoded
                else:
                    capabilities.update({'firefox_profile': browser_profile.encoded})
            w3c_caps = _make_w3c_caps(capabilities)
            parameters = {"capabilities": w3c_caps,
                          "desiredCapabilities": capabilities}
    

    收获;对于selenium 的执行流程有了更加深刻的了解,遇见问题不要慌,一步一步打断点慢慢找总能清除掉

    相关文章

      网友评论

          本文标题:selenium 分布式执行补充--填坑1

          本文链接:https://www.haomeiwen.com/subject/ivuvqctx.html