V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
LSB
V2EX  ›  问与答

Python Selenium 问题

  •  
  •   LSB · 2023-06-06 07:57:41 +08:00 · 1076 次点击
    这是一个创建于 561 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下

    from selenium import webdriver
    
    import time
    # 打开浏览器
    #driver = webdriver.Chrome()
    driver = webdriver.Firefox()
    #driver = webdriver.Edge()
    # 设置浏览器最大化
    driver.maximize_window()
    # 设置隐式等待
    driver.implicitly_wait(3)
    # 打开网页
    driver.get("http://www.baidu.com")
    

    出现的错误

    Traceback (most recent call last):
      File "C:\Users\kkk\PycharmProjects\pythonProject\main.py", line 8, in <module>
        driver = webdriver.Firefox()
                 ^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 170, in __init__
        RemoteWebDriver.__init__(
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
        self.start_session(capabilities, browser_profile)
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
        response = self.execute(Command.NEW_SESSION, parameters)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
        response = self.command_executor.execute(driver_command, params)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 374, in execute
        return self._request(command_info[0], url, body=data)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 397, in _request
        resp = self._conn.request(method, url, body=body, headers=headers)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\_request_methods.py", line 118, in request
        return self.request_encode_body(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\_request_methods.py", line 217, in request_encode_body
        return self.urlopen(method, url, **extra_kw)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\poolmanager.py", line 422, in urlopen
        conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\poolmanager.py", line 303, in connection_from_host
        return self.connection_from_context(request_context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\poolmanager.py", line 328, in connection_from_context
        return self.connection_from_pool_key(pool_key, request_context=request_context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\poolmanager.py", line 351, in connection_from_pool_key
        pool = self._new_pool(scheme, host, port, request_context=request_context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\poolmanager.py", line 265, in _new_pool
        return pool_cls(host, port, **request_context)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 196, in __init__
        timeout = Timeout.from_float(timeout)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\timeout.py", line 190, in from_float
        return Timeout(read=timeout, connect=timeout)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\timeout.py", line 119, in __init__
        self._connect = self._validate_timeout(connect, "connect")
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\kkk\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\timeout.py", line 156, in _validate_timeout
        raise ValueError(
    ValueError: Timeout value connect was <object object at 0x000001E6DFDE4530>, but it must be an int, float or None.
    

    三个浏览器都试过了,都是同样的问题,有小伙伴帮看看么

    6 条回复    2023-06-07 09:22:38 +08:00
    tulongtou
        1
    tulongtou  
       2023-06-06 08:13:04 +08:00
    urllib3 怎么安装的?版本不匹配吧,是不是安装了最新版的 urllib3 ,最新版 urllib3 有很多 breaking changes
    beixiao
        2
    beixiao  
       2023-06-06 08:24:07 +08:00 via iPhone
    驱动按照版本装了吗
    LSB
        3
    LSB  
    OP
       2023-06-06 08:51:56 +08:00
    @beixiao 按照版本装了,也设置环境变量了
    LSB
        4
    LSB  
    OP
       2023-06-06 08:52:14 +08:00
    @tulongtou 我降级版本看看
    1d074bfa18d34f6c
        5
    1d074bfa18d34f6c  
       2023-06-06 09:25:10 +08:00
    语法没有问题,应该是 Selenium 或者 Webdriver 版本问题,建议卸载重装
    LSB
        6
    LSB  
    OP
       2023-06-07 09:22:38 +08:00
    @tulongtou 谢谢 果然是 urllib3 版本问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1469 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 150ms · UTC 17:12 · PVG 01:12 · LAX 09:12 · JFK 12:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.