How to execute Selenium Chrome WebDriver in silent mode?

前端 未结 8 1591
广开言路
广开言路 2020-11-30 06:54

When using Chrome Selenium-WebDriver, it will output diagnostic output when the servers are started:

Started ChromeDriver (v2.0) on port 9515

相关标签:
8条回答
  • 2020-11-30 07:01

    try this code it will hide browser with "headless" Argument but Chrome ver should > 58

    ( and even you can hide command prompt window )

        IWebDriver driver;
    ChromeOptions options = new ChromeOptions();
    options.AddArguments("--disable-extensions");
    options.AddArgument("test-type");
    options.AddArgument("--ignore-certificate-errors");
    options.AddArgument("no-sandbox");
    options.AddArgument("--headless");//hide browser
    
    ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"chromedriverExepath\");
    service.SuppressInitialDiagnosticInformation = true;
    //service.HideCommandPromptWindow = true;//even we can hide command prompt window (with un comment this line)  
    options.BinaryLocation = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
    driver = new ChromeDriver(service, options);
    
    driver.Manage().Window.Maximize();
    driver.Navigate().GoToUrl("https://www.example.com");
    
    0 讨论(0)
  • 2020-11-30 07:01

    This code works fine for me:

    public static IWebDriver Driver { set; get; }
    -----
    Driver = CreateBrowserDriver();
    
    ////////////// Create Driver
    private static IWebDriver CreateBrowserDriver()
    {
        try
        {
            var options = new OpenQA.Selenium.Chrome.ChromeOptions();
            options.AddArguments("--disable-extensions");
            options.AddArgument("--headless"); // HIDE Chrome Browser
            var service = OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService();
            service.HideCommandPromptWindow = true; // HIDE Chrome Driver
            service.SuppressInitialDiagnosticInformation = true;
    
            return new OpenQA.Selenium.Chrome.ChromeDriver(service, options);
        }
        catch
        {
            throw new Exception("Please install Google Chrome.");
        }
    }
    ////////////// Exit Driver
    public static void ExitDriver()
    {
        if (Driver != null)
        {
            Driver.Quit();
        }
    
        Driver = null;
    
        try
        {
            // Chrome
            System.Diagnostics.Process.GetProcessesByName("chromedriver").ToList().ForEach(px => px.Kill());
        }
        catch { }
    }
    
    0 讨论(0)
  • 2020-11-30 07:04

    For me no one of previous answers did not help , my solution was:

    ChromeDriverService service = ChromeDriverService.CreateDefaultService(driverLocation);
    service.SuppressInitialDiagnosticInformation = true;
    service.HideCommandPromptWindow = true;
    var driver = new ChromeDriver(service, options);
    
    0 讨论(0)
  • 2020-11-30 07:09

    Good question, however, I don't know where you got that .AddArgument("--silent"); thing, as that's Chrome's command line switch, not for ChromeDriver. Also, there isn't a Chrome switch called --silent anyway.

    Under OpenQA.Selenium.Chrome namespace, there is class called ChromeDriverService which has a property SuppressInitialDiagnosticInformation defaults to false. Basically what you might want to do is to create ChromeDriverService and pass it into ChromeDriver's constructor. Please refer to the documentation here.

    Here is the C# code that suppresses ChromeDriver's diagnostics outputs.

    ChromeOptions options = new ChromeOptions();
    
    ChromeDriverService service = ChromeDriverService.CreateDefaultService();
    service.SuppressInitialDiagnosticInformation = true;
    
    IWebDriver driver = new ChromeDriver(service, options);
    

    EDIT: ChromeDriver (not Chrome) has a command line argument --silent, which is supposed to work. SuppressInitialDiagnosticInformation in .NET binding does exactly that. However, it seems only suppress some of the messages.

    Here is a closed chromedriver ticket: Issue 116: How to disable the diagnostic messages and log file from Chrome Driver?

    0 讨论(0)
  • 2020-11-30 07:14

    For anyone finding themselves here wanting a Java solution, there is a thread here:

    Selenium chromedriver disable logging or redirect it java

    0 讨论(0)
  • 2020-11-30 07:15

    I simply do this

    ChromeOptions options = new ChromeOptions();
    options.AddArgument("--log-level=3");
    IWebDriver driver = new ChromeDriver(options);
    
    0 讨论(0)
提交回复
热议问题