提问人:femiir 提问时间:9/6/2021 更新时间:9/8/2021 访问量:466
如何在 Selenium 中添加 chrome 选项以禁用警报
how to add chrome options to disable alerts in selenium
问:
嘿社区,我正在遵循 freecodecamp 的 selenium 教程,我正在使用 Facebook 进行测试,我正在使用 MacBook,我使用 brew 安装了我的 chromedriver
我遇到了我的第一个问题是警报,我想禁用警报,但不知道如何将其添加到我的 chrome 中,因为它在没有声明驱动程序路径的情况下自行打开,请帮助所有答案显示正在声明的路径并使用 chrome 选项禁用它,但我继承自 Webdriver.Chrome 添加一个新的 chrome,例如每次运行它时都会创建两个 chrome 实例使用下面的代码片段,我需要帮助使用 chromeoptions 禁用警报driver = 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)```
答:
0赞
femiir
9/8/2021
#1
所以我在学习了更多关于课程的知识后能够通过这个......我只是希望我正在做的是pythonic的,所以这里是为我施展魔法的代码片段。
我将选项传递给类的 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)
评论