如何在 Selenium 中處理 Chrome 通知?


我們可以在 Selenium webdriver 中處理 Chrome 通知。這些通知通常稱為 web 推送通知,可以透過 ChromeOptions 類的幫助進行處理。下圖顯示了來自瀏覽器 Chrome 的通知 -

我們必須建立這個 ChromeOptions 類的物件,並在其上應用 addArguments 方法。引數 --disable-notifications 被傳遞為該方法的引數。最後,這些資訊被髮送到 ChromeDriver。

語法

ChromeOptions o = new ChromeOptions();
o.addArguments("--disable-notifications");

示例

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.WebDriver;
public class NotificationChrome {
   public static void main(String[] args) throws IOException {
      //object of ChromeOptions
      ChromeOptions o = new ChromeOptions();

      //set browser options o.addArguments("--disable-notifications");
      System.setProperty("webdriver.chrome.driver",
         "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
      // pass browser option to webdriver
      WebDriver driver = new ChromeDriver(o);
      //implicit wait
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      //URL launch
      driver.get("https://www.justdial.com/Bangalore/Bakeries");
   }
}

輸出

更新於: 08-Apr-2021

5000+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告