使用InstaPy开发Instagram机器人遇WebDriver参数错误求助
问题:InstaPy运行时触发TypeError错误
我的代码:
from instapy import InstaPy session = InstaPy(username = "username", password = "password") session.login() session.set_relationship_bounds(enabled = True, max_followers = 1000) session.set_do_follow(True, percentage=100) session.like_by_tags(["clothingbrand","clothing","fashion"], amount = 3) session.set_dont_like(["nsfw"]) session.end()
报错信息:
Workspace in use: "C:/Users/1/InstaPy" Traceback (most recent call last): File "C:\Instagram\instagram_bot.py", line 3, in <module> session = InstaPy(username = "username", password = "password") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\1\AppData\Local\Programs\Python\Python311\Lib\site-packages\instapy\instapy.py", line 330, in __init__ self.browser, err_msg = set_selenium_local_session( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\1\AppData\Local\Programs\Python\Python311\Lib\site-packages\instapy\browser.py", line 123, in set_selenium_local_session browser = webdriver.Firefox( ^^^^^^^^^^^^^^^^^^ TypeError: WebDriver.__init__() got an unexpected keyword argument 'firefox_profile'
解决方案
这个错误是因为你安装的Selenium版本(4.x+)已移除firefox_profile参数,而旧版InstaPy代码仍在使用该废弃参数,导致版本不兼容。可通过以下三种方式修复:
1. 降级Selenium到兼容版本
打开命令行执行:
pip uninstall selenium -y pip install selenium==3.141.0
该版本是与多数旧版InstaPy兼容的稳定版本。
2. 安装InstaPy最新开发版
官方开发分支可能已修复Selenium 4.x兼容问题,执行:
pip uninstall instapy -y pip install git+https://github.com/InstaPy/InstaPy.git
3. 手动修改InstaPy源码(不推荐新手)
找到报错的browser.py文件(路径:C:\Users\1\AppData\Local\Programs\Python\Python311\Lib\site-packages\instapy\browser.py),找到创建Firefox驱动的代码段,将firefox_profile参数替换为options:
- 先导入FirefoxOptions:
from selenium.webdriver.firefox.options import Options - 替换原代码:
# 原代码 browser = webdriver.Firefox( firefox_profile=profile, executable_path=geckodriver_path, firefox_options=options, proxy=proxy, ) # 修改后 firefox_options = Options() firefox_options.profile = profile browser = webdriver.Firefox( executable_path=geckodriver_path, options=firefox_options, proxy=proxy, )
保存文件后重新运行代码即可。
内容的提问来源于stack exchange,提问作者Oh Well YuhAmobi
相关产品推荐
相关产品推荐

