python 未检测到Chromedriver,AttributeError:“ChromeOptions”对象没有属性“headless”

rt4zxlrg  于 2023-09-29  发布在  Python
关注(0)|答案(1)|浏览(669)

我在运行Python Selenium脚本打开网页时遇到错误。我已经尝试卸载并重新安装selenium、chromeautoinstaller和未检测到的chromedriver。我也尝试添加option.add_argument('--headless')。这些都没有成功,错误仍然相同。
下面是我的代码:

def driverInit():
        option = uc.ChromeOptions()
        option.add_argument("--log-level=3")
    
        prefs = {"credentials_enable_service": False,
                 "profile.password_manager_enabled": False,
                 "profile.default_content_setting_values.notifications": 2
                 }
        option.add_experimental_option("prefs", prefs)
        driverr = uc.Chrome(options=option)
        return driverr
    
    def driverInitBuffMarket():
        option = uc.ChromeOptions()
        option.add_argument(
             rf'--user-data-dir=C:\Users\{os.getlogin()}\AppData\Local\Google\ChromeBuff\User Data')  # e.g. C:\Users\You\AppData\Local\Google\Chrome\User Data
        option.add_argument(r'--profile-directory=Default')
        driverr = uc.Chrome(options=option)
        return driver

错误发生在倒数第二行driverr = uc.Chrome(options=option)
下面是错误:

Traceback (most recent call last):
  File "C:\Users\kumpd\OneDrive\Desktop\All Market Bots\BuffMarket Purchase Bot Testing\main.py", line 266, in <module>
    start_buy_monitoring()
  File "C:\Users\kumpd\OneDrive\Desktop\All Market Bots\BuffMarket Purchase Bot Testing\main.py", line 207, in start_buy_monitoring
    driverBuffMarket = driverInitBuffMarket()
                       ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kumpd\OneDrive\Desktop\All Market Bots\BuffMarket Purchase Bot Testing\main.py", line 42, in driverInitBuffMarket
    driverr = uc.Chrome(options=option)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\kumpd\AppData\Local\Programs\Python\Python311\Lib\site-packages\undetected_chromedriver\__init__.py", line 398, in __init__
    if headless or options.headless:
                   ^^^^^^^^^^^^^^^^
AttributeError: 'ChromeOptions' object has no attribute 'headless'

任何帮助都非常感谢!

axr492tv

axr492tv1#

到今天为止,undetected-chromedriver仍然在他们的代码中使用options.headless
这是一个问题,因为刚刚发布的selenium4.13.0中的此更改:

* remove deprecated headless methods

以下是一些备选方案:

  • 降级到早期的selenium版本,直到修复。
  • 使用SeleniumBaseUC Mode(undetected-chromedriver的修改后的分支):
from seleniumbase import Driver
import time

driver = Driver(uc=True)
driver.get("https://nowsecure.nl/#relax")
time.sleep(6)
driver.quit()

下面是另一个更高级的重试和单击示例(使用SB()管理器):

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.driver.get("https://nowsecure.nl/#relax")
    sb.sleep(2)
    if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
        sb.get_new_driver(undetectable=True)
        sb.driver.get("https://nowsecure.nl/#relax")
        sb.sleep(2)
    if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
        if sb.is_element_visible('iframe[src*="challenge"]'):
            with sb.frame_switch('iframe[src*="challenge"]'):
                sb.click("span.mark")
                sb.sleep(4)
    sb.activate_demo_mode()
    sb.assert_text("OH YEAH, you passed!", "h1", timeout=3)

(This主要是我在这里写的:https://github.com/ultrafunkamsterdam/undetected-chromedriver/issues/1584#issuecomment-1737963363)

相关问题