请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

启动浏览器报错了,这个是什么原因

from selenium.webdriver import *
from typing import Type, Union


class BrowserTypeError(Exception):

    def __init__(self, _type):
        self._type = _type

    def __str__(self):
        return f'unsupported browser type:{self._type}'


class Browser:
    Chrome_Driver_Path = './drivers/chrome_driver/chrome_driver.exe'
    Edge_Driver_Path = './drivers/edge_driver/edge_driver.exe'
    IE_Driver_Path = './drivers/IEDriverServer/IEDriverServer.exe'


    WINDOWS_SIZE = (1024, 768)

    IMP_TIME = 30

    PAGE_LOAD_TIME = 20

    SCRIPT_TIME_OUT = 20

    HEADLESS = True

    def __init__(self, browser_type: Type[Union[Firefox, Chrome, Ie, Edge]] = Chrome,
                 option_type: Type[Union[FirefoxOptions, ChromeOptions, IeOptions]] = ChromeOptions,
                 driver_path: str = Chrome_Driver_Path):
        if not issubclass(browser_type, (Firefox, Chrome, Ie, Edge)):
            raise BrowserTypeError(browser_type)
        if not issubclass(option_type, (FirefoxOptions, ChromeOptions, IeOptions)):
            raise BrowserTypeError(option_type)
        if not isinstance(driver_path, str):
            raise TypeError
        self._path = driver_path
        self._browser = browser_type
        self._option = option_type

    @property
    def options(self):
        """
        浏览器特定的操作,在子类中实现
        :return:
        """
        return

    @property
    def browser(self):
        """
        启动浏览器,返回浏览器实例
        :return:
        """
        return


class Chrome(Browser):

    OPTION_MARK = True

    METHOD_MARK = True

    HEADLESS = False

    IMP_TIME = 30

    PAGE_LOAD_TIME = 30

    SCRIPT_TIME_OUT = 30

    WINDOWS_SIZE = (1920, 900)

    START_MAX = '--start-maximized'

    EXP = {
        'excludeSwitches': ['enable-automation'],
        'mobileEmulation': {'deviceName': 'iPhone6'}
    }

    @property
    def options(self):
        if self.OPTION_MARK:
            chrome_option = self._option()
            chrome_option.add_argument(self.START_MAX)
            for k, v in self.EXP.items():
                chrome_option.add_experimental_option(k, v)
            chrome_option.headless = self.HEADLESS
            return chrome_option
        return None

    @property
    def browser(self):
        if self.options:
            chrome = self._browser(self._path, options=self.options)
        else:
            chrome = self._browser(self._path)

        if self.METHOD_MARK:
            chrome.implicitly_wait(self.IMP_TIME)
            chrome.set_script_timeout(self.SCRIPT_TIME_OUT)
            chrome.set_page_load_timeout(self.PAGE_LOAD_TIME)
            # chrome.set_window_size(*self.WINDOWS_SIZE)
        return chrome


with Chrome().browser as _chrome:
    _chrome.get('http://127.0.0.1/zentao/user-login.html')
    from time import sleep
    sleep(3)

以下是报错截图
图片描述

正在回答 回答被采纳积分+3

2回答

托尼老师 2023-03-31 11:30:34

4.4.3版本的bug:

https://img1.sycdn.imooc.com//szimg/642653b40968b99b07780889.jpg

0 回复 有任何疑惑可以回复我~
托尼老师 2023-03-30 11:48:11

同学,你的selenium是哪个版本?

0 回复 有任何疑惑可以回复我~
  • 提问者 weixin_慕哥3021856 #1
    4.4.3
    回复 有任何疑惑可以回复我~ 2023-03-30 11:51:08
  • 托尼老师 回复 提问者 weixin_慕哥3021856 #2
    升级到4.8.3即可。原因:4.4.3版本的selenium.webdriver包的可访问属性列表__all__中定义了Opera对象,但导入的对象中又没有,所以导入*就报错了。这里是个bug,详见截图。
    回复 有任何疑惑可以回复我~ 2023-03-31 11:29:17
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信