10101
10101

Reputation: 2412

Hide Chrome window while scraping websites

I have been working on Web crawler for a while. Now everything works fine but I would like to add final touch to my program and hide Chrome windows from being visible while processes are running.

I have tried to add this one to my code:

var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
return new ChromeDriver(chromeDriverService,  new ChromeOptions());

but there has been no success. Can somebody give me a hint how this hide command should be added to work correct? How should I modify my current code to implement background running?

Here is my code:

using System.Linq;
using OpenQA.Selenium.Chrome;

namespace WebDriverTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize the Chrome Driver
            using (var driver = new ChromeDriver())
            {
                // Go to the home page
                driver.Navigate().GoToUrl("xxx.com");
                driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
                // Get the page elements
                var userNameField = driver.FindElementById("loginForm:username");
                var userPasswordField = driver.FindElementById("loginForm:password");
                var loginButton = driver.FindElementById("loginForm:loginButton");

                // Type user name and password
                userNameField.SendKeys("username");
                userPasswordField.SendKeys("password");

                // and click the login button
                loginButton.Click();

                // Extract the text and save it into result.txt
                // var result = driver.FindElementByXPath("//div[@id='case_login']/h3").Text;
                // File.WriteAllText("result.txt", result);

                // Take a screenshot and save it into screen.png
                driver.GetScreenshot().SaveAsFile(@"screen.png", OpenQA.Selenium.ScreenshotImageFormat.Png);
            }
        }

    }
}

Upvotes: 1

Views: 1164

Answers (2)

Matt Williams
Matt Williams

Reputation: 116

Have you tried adding:

var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");

Upvotes: 3

10101
10101

Reputation: 2412

Ok, after some investigation, it should be:

using System.Linq;
using OpenQA.Selenium.Chrome;
using Scripting;
using System.IO;

namespace WebDriverTest
{
    class Program
    {
        static void Main(string[] args)
        {

            var chromeOptions = new ChromeOptions();
            chromeOptions.AddArguments("headless");

            // Initialize the Chrome Driver
            using (var driver = new ChromeDriver(chromeOptions))
            {
                // Go to the home page
                driver.Navigate().GoToUrl("xxx.com");
                driver.Manage().Timeouts().ImplicitWait = System.TimeSpan.FromSeconds(15);
                // Get the page elements
                var userNameField = driver.FindElementById("loginForm:username");
                var userPasswordField = driver.FindElementById("loginForm:password");
                var loginButton = driver.FindElementById("loginForm:loginButton");

                // Type user name and password
                userNameField.SendKeys("username");
                userPasswordField.SendKeys("password");

                // and click the login button
                loginButton.Click();

                // Extract the text and save it into result.txt
                // var result = driver.FindElementByXPath("//div[@id='case_login']/h3").Text;
                // File.WriteAllText("result.txt", result);

                // Take a screenshot and save it into screen.png
                driver.GetScreenshot().SaveAsFile(@"screen.png", OpenQA.Selenium.ScreenshotImageFormat.Png);
           }
        }

    }
}

Upvotes: 0

Related Questions