jimdevops19 / SeleniumSeries

This is the repository for my Selenium Series
138 stars 88 forks source link

how to use chrome options to disable alerts #1

Open femiir opened 3 years ago

femiir commented 3 years ago

hey Jim I am following your tutorial on selenium I am using Facebook to test and I am using a MacBook I installed my chrome driver 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


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)```
femiir commented 3 years ago

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

please tell me if this is efficient and the pythonic way of doing this


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)