selenium-webdriver - Selenium Remote Webdriver 给出 JSONDecodeError

标签 selenium-webdriver python-3.5

我的代码:

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 但它也失败了。

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

最佳答案

我知道这是 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('\\'', '\\"')

关于selenium-webdriver - Selenium Remote Webdriver 给出 JSONDecodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33427246/

相关文章:

python - 在 Python 中将正则表达式与 fileinput 结合使用会出现错误

java - java中selenium可以同时操作多个tab吗?

windows - 如何使用 bash 命令在 Windows 10 中创建和激活虚拟环境

java - TestNG 中的非静态 Webdriver 驱动程序和屏幕截图监听器

perl - 如何使用 perl 语言在 selenium 远程驱动程序中打开新选项卡并打开 url

python - 如果字典有多个值,如何使用 try except ?

algorithm - 时间序列的tensorflow lstm模型

python - Anaconda 中 Keras 的包安装?

c# - 切换选项卡在 Selenium Webdriver 中不起作用

selenium - 以另一种方式检查 'checkBox' -> xPath