【问题标题】:how to add chrome options to disable alerts in selenium如何添加 chrome 选项以禁用 selenium 中的警报
【发布时间】:2021-09-06 13:39:59
【问题描述】:

嘿社区我正在关注来自 freecodecamp 的硒教程,我正在使用 Facebook 进行测试我正在使用 MacBook 我使用 brew 安装了我的 chromedriver

我遇到了第一个警报问题,我想禁用警报,但不知道如何将其添加到我的 chrome,因为它在没有声明驱动程序路径的情况下自行打开,请帮助所有答案显示正在声明的路径和使用 chrome 选项禁用它,但我是从 Webdriver.Chrome 继承的

import facebook.constants as const
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time

class FacebookBot(webdriver.Chrome):
    """a class to control and automate a facebook bot for srappping"""
    def __init__(self, teardown=False):
        self.teardown = teardown
        super(FacebookBot, self).__init__()
       
        
        self.implicitly_wait(20)

    def __exit__(self, *args) -> None:
        if self.teardown:
            self.quit()
            return super().__exit__(*args)
         

    def facebook_homepage(self):
        """navigating the facebook scrapper bot to the facebook home page."""
        self.get(const.BASE_URL)```

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver web-scraping chrome-web-driver


    【解决方案1】:

    所以我在学习了更多关于类的知识后能够通过这个...我只是希望我所做的是 pythonic 所以这里是为我创造了魔法的代码 sn-p。

    我将选项传递给类的init方法

    import facebook.constants as const
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import time
    import os
    
    
    class FacebookBot(webdriver.Chrome):
        """a class to control and automate a facebook bot for srappping"""
        def __init__(self, driver_path=r"/opt/homebrew/bin/chromedriver", teardown=False):
            options = Options()
            options.add_argument("--disable-notifications")
            self.driver_path = driver_path   
            os.environ["PATH"] += self.driver_path
            self.teardown = teardown
            super(FacebookBot, self).__init__(options=options)
            # self.maximize_window()
            self.implicitly_wait(20)
    
        def __exit__(self, *args) -> None:
            if self.teardown:
                self.quit()
                return super().__exit__(*args)
             
    
        def facebook_homepage(self):
            """navigating the facebook scrapper bot to the facebook home page."""
            self.get(const.BASE_URL)
       
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 2020-03-21
      • 1970-01-01
      • 2021-11-15
      • 2020-03-21
      相关资源
      最近更新 更多