【问题标题】:Handle notifications in Python + Selenium Chrome WebDriver在 Python + Selenium Chrome WebDriver 中处理通知
【发布时间】:2017-05-15 00:46:11
【问题描述】:

如何在 Python 中处理 Selenium Chrome WebDriver 通知?

已尝试关闭/接受alertactive element,但似乎必须以其他方式处理通知。此外,所有 Google 搜索结果都在驱使我使用我并不真正需要的 Java 解决方案。我是 Python 的新手。

提前致谢。

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    2021 年 12 月,使用 ChromeDriver 版本:96python 代码结构将如下所示来处理 ChromeDriver/浏览器通知:

    from selenium import webdriver    
    from selenium.webdriver.chrome.options import Options
    
    # Creating Instance
    option = Options()
    
    # Working with the 'add_argument' Method to modify Driver Default Notification
    option.add_argument('--disable-notifications')
    
    # Passing Driver path alongside with Driver modified Options
    browser = webdriver.Chrome(executable_path= your_chrome_driver_path, chrome_options= option)
    
    # Do your stuff and quit your driver/ browser
    browser.get('https://www.google.com')
    browser.quit()
    

    【讨论】:

      【解决方案2】:

      按照以下代码使用 Python selenium 处理通知设置

      from selenium import webdriver
      
      
      from selenium.webdriver.chrome.options import Options
      
      option = Options()
      
      option.add_argument("--disable-infobars")
      
      option.add_argument("start-maximized")
      
      option.add_argument("--disable-extensions")
      
      option.add_experimental_option("prefs", 
      {"profile.default_content_setting_values.notifications": 2 
       }) 
      

      注意 1-允许通知,2-阻止通知

      【讨论】:

        【解决方案3】:

        使用 Chrome 版本 85.0.4183.83(官方版本)64 位和 ChromeDriver 85.0.4183.83

        这是代码,它正在工作

        from selenium import webdriver
        
        chrome_options = webdriver.ChromeOptions()
        prefs = {"profile.default_content_setting_values.notifications" : 2}
        chrome_options.add_experimental_option("prefs",prefs)
        driver = webdriver.Chrome(chrome_options=chrome_options)
        driver.get("https://www.google.com/")
        

        2020/08/27

        【讨论】:

          【解决方案4】:

          上面 16 年 12 月的答案在 2020 年 8 月对我不起作用。我相信 Selenium 和 Chrome 都发生了变化。

          我正在使用 Chrome 84 的二进制文件,它被列为当前版本,尽管 selenium.dev 提供了 Chrome 85 的二进制文件。我不得不假设这是一个测试版,因为我没有其他信息。

          到目前为止,我只有部分答案,但我相信以下两行的方向是正确的。他们是我今晚所到的。我有一个向 Facebook 打开的测试窗口,并在问题中显示了确切的窗口。上面 16 年 12 月 30 日 pythonjar 的回答中给出的代码为我产生了错误消息。这些代码行没有错误,所以我相信这是正确的轨道。

          >>> from selenium.webdriver import ChromeOptions
          >>> chrome_options = ChromeOptions()
          

          如果我有时间,我会在明天更新。

          对不起,部分答案。我希望尽快完成它。我希望这有帮助。如果你先到那里,请告诉我。

          【讨论】:

            【解决方案5】:

            您可以使用 chrome 选项禁用浏览器通知。

            chrome_options = webdriver.ChromeOptions()
            prefs = {"profile.default_content_setting_values.notifications" : 2}
            chrome_options.add_experimental_option("prefs",prefs)
            driver = webdriver.Chrome(chrome_options=chrome_options)
            

            【讨论】:

            • 但我只想禁用 xdg-open,而不是所有通知。我该如何解决?
            猜你喜欢
            • 1970-01-01
            • 2019-05-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-04-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多