python-3.xseleniumselenium-webdriverweb-scrapingchrome-web-driver

how to add chrome options to disable alerts in selenium


hey community I am following a tutorial on selenium from freecodecamp and I am using Facebook to test I am using a MacBook I installed my chromedriver using brew

I run into my first issue which is alert and I want to disable alerts and have no idea how to add it to my chrome since it opens on its own without declaring a driver path please help all answers shows a path being declared and using chrome options to disable it but I am inheriting from Webdriver.Chrome adding a new chrome such as driver = Webdriver.Chrome() creates two instances of chrome each time I run it using the snippet of my code below I need help using chromeoptions to disable alerts

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)```

Solution

  • so I was able to pass this after studying more about classes...I just hope what I am doing is pythonic so here is the code snippet that did the magic for me.

    I passed the option to the init method of the class

    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)