4

我的代码:

import unittest
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common import desired_capabilities

class test_mycode(unittest.TestCase):
    def setUp(self):
        desired_cap = {'browser': 'IE', 'browser_version': '8.0', 'os': 'Windows', 'os_version': '7', 'resolution': '1366x768'}
        self.driver = webdriver.Remote(command_executor='http://username:ACCESS_KEY@hub.browserstack.com:80/wd/hub', desired_capabilities=desired_cap)

    def test_website(self):    
    .....

错误是:

Traceback (most recent call last):
File "C:\Workspace\Pumpkin Patch Website\test cases\_test_mycode.py", line 55, in setUp
    desired_capabilities=desired_cap)
File "C:\Users\elton.tiong\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium-2.48.0-py3.5.egg\selenium\webdriver\remote\webdriver.py", line 87, in __init__
    self.start_session(desired_capabilities, browser_profile)
File "C:\Users\elton.tiong\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium-2.48.0-py3.5.egg\selenium\webdriver\remote\webdriver.py", line 141, in start_session
    'desiredCapabilities': desired_capabilities,
File "C:\Users\elton.tiong\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium-2.48.0-py3.5.egg\selenium\webdriver\remote\webdriver.py", line 201, in execute
    self.error_handler.check_response(response)
File "C:\Users\elton.tiong\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium-2.48.0-py3.5.egg\selenium\webdriver\remote\errorhandler.py", line 102, in check_response
    value = json.loads(value_json)
File "C:\Users\elton.tiong\AppData\Local\Programs\Python\Python35-32\lib\json\__init__.py", line 319, in loads
    return _default_decoder.decode(s)
File "C:\Users\elton.tiong\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\elton.tiong\AppData\Local\Programs\Python\Python35-32\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

selenium-server-standalone2.48.0 已安装并正在运行。

Python 是 3.5

这是我为在 Browserstack 上运行 Selenium 而编写的代码。我按照 Browserstack 给出的说明进行操作,但是,当我运行它时,它引发了 JSONDecodeError。

我还将 command_executor 更改为http://127.0.0.1:4444/wd/hub但失败了。然后我将desired_capabilities=desired_cap 更改为desired_capabilities.DesireCapabilities.FIREFOX 但它也失败了。

我到处搜索,但找不到任何答案。有人可以帮忙吗?非常感谢。

4

1 回答 1

2

我知道这是 2 岁,但由于没有人回答,希望这对其他人有所帮助。中的值desired_cap基本上是无效的 json。为了确认,我针对https://jsonlint.com/运行了它。

修复很简单 - 将所有单引号替换为双引号:

desired_cap = {'browser': 'IE', 'browser_version': '8.0', 'os': 'Windows', 'os_version': '7', 'resolution': '1366x768'}
desired_cap = desired_cap.replace('\\'', '\\"')
于 2017-10-02T01:15:38.697 回答