首页 > 解决方案 > Selenium 'dict' 对象没有属性 'to_capabilities'

问题描述

我正在开发一个自动机器人测试项目来抓取和测试 booking.com 网站,我的文件夹结构为

   BotScraper
      -bookings
       - __init__.py
       - booking.py
       - constants.py
      -main.py

main.py 代码

from bookings.booking import Booking
# create instance for Booking class
bot = Booking() 
bot.landing_page()

预订.py 代码

import bookings.constants as const
from selenium import webdriver


# Inherited webdriver.Chrome(superclass) w.r.t booking class(child class)
class Booking(webdriver.Chrome(r"C:\SeleniumDrivers\chromedriver.exe")):

# constructor for driver path default is mentioned
def __init__(self, driver_path=r"C:\SeleniumDrivers\chromedriver.exe"):
    self.driver_path = driver_path
    
    # webdriver.chrome class methods accessible now
    super(Booking, self).__init__()

def landing_page(self):
    # self. (all methods for webdriver.chrome now accessible)
    self.get(const.TARGET_BASE_URL)

常量.py 代码

TARGET_BASE_URL = "https://www.booking.com"

当我执行 main.py 然后浏览器打开但是,代码停止执行并返回错误

Traceback (most recent call last):
 File "C:/Users/wangu/Desktop/Testing_Practices/Selenium/seBotScraper/main.py", line 
 1, in <module>
 from bookings.booking import Booking
 File 
"C:\Users\wangu\Desktop\Testing_Practices\Selenium\seBotScraper\bookings\booking.py", 
line 7, in <module>
class Booking(webdriver.Chrome(r"C:\SeleniumDrivers\chromedriver.exe")):
File "C:\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 64, 
in __init__
desired_capabilities = options.to_capabilities()
AttributeError: 'dict' object has no attribute 'to_capabilities'

进程以退出代码 1 结束

知道可能是什么原因导致此错误及其解决方案。

标签: pythonseleniumselenium-webdriver

解决方案


推荐阅读