首页 > 解决方案 > Python Selenium TypeError: __init__() 接受 1 个位置参数,但给出了 2 个

问题描述

我的网络爬虫不起作用。它给了我这个错误,“TypeError:init () 需要 1 个位置参数,但给出了 2 个。”

当我尝试使用 Edge、Chrome 和 Firefox 打开时出现该错误。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Edge(executable_path="C:\\Users\.......\Python\Python37-32\Lib\site-packages\selenium\edgedriver_win64\msedgedriver.exe")
driver.set_page_load_timeout(30)
driver.get("https://www.udemy.com/topic/python/")
driver.quit()

我查看了其他人的示例代码,它看起来与我所做的相同。

另外,程序在 D 盘,而 edgedriver_win64 在 C 盘。我不知道这是否会有所作为。

这是完整的堆栈跟踪:

Traceback (most recent call last):
  File "D:\Programs and STEM\Python\Self-Taught Programmer\udemy_course_scraper.py", line 57, in <module>
    scrape.scrape_website()
  File "D:\Programs and STEM\Python\Self-Taught Programmer\udemy_course_scraper.py", line 22, in scrape_website
    driver = webdriver.Edge(executable_path="C:\\Users\Alex\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\selenium\edgedriver_win64\msedgedriver.exe")
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\edge\webdriver.py", line 66, in __init__
    desired_capabilities=capabilities)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 319, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\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\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 402, in _request
    resp = http.request(method, url, body=body, headers=headers)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\request.py", line 72, in request
    **urlopen_kw)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\request.py", line 150, in request_encode_body
    return self.urlopen(method, url, **extra_kw)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\poolmanager.py", line 315, in urlopen
    conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\poolmanager.py", line 231, in connection_from_host
    return self.connection_from_context(request_context)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\poolmanager.py", line 244, in connection_from_context
    return self.connection_from_pool_key(pool_key, request_context=request_context)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\poolmanager.py", line 265, in connection_from_pool_key
    pool = self._new_pool(scheme, host, port, request_context=request_context)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\poolmanager.py", line 199, in _new_pool
    return pool_cls(host, port, **request_context)
  File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\connectionpool.py", line 179, in __init__
    self.pool = self.QueueCls(maxsize)
TypeError: __init__() takes 1 positional argument but 2 were given

标签: pythonseleniumwebdriver

解决方案


我在 webdriver 模块找到驱动程序的确切位置时遇到了几个问题,即使它在同一个文件夹中也是如此。这是因为如果您将其作为脚本运行,我相信 python 本身会首先检查其自己的脚本文件夹以查找任何类型的驱动程序可执行文件。

我可以通过将 msedgedriver.exe、chromedriver.exe 等放在我的 python 脚本文件夹中来解决这个问题。我希望这有帮助。然后我就可以跑了 driver = webdriver.Edge()

C:\Program Files\Python37\Scripts

推荐阅读