1

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);
            }
        }

    }
}
10101
  • 2,232
  • 3
  • 26
  • 66
  • What is your program trying to do? Are you sure it is not a better idea to use some kind of an API to log in? – the default. Nov 26 '19 at 15:06
  • Currently it is logging in to page, later I will add file download after login – 10101 Nov 26 '19 at 15:09
  • 1
    Does this answer your question? [Hide/Silence ChromeDriver window](https://stackoverflow.com/questions/35818436/hide-silence-chromedriver-window) – Selvin Nov 26 '19 at 15:13
  • ... which is 1st google result for "Hide Chrome window Selenium" – Selvin Nov 26 '19 at 15:14
  • @Selvin you can edit comments for 5 minutes after posting them – the default. Nov 26 '19 at 15:14
  • @mypronounismonicareinstate first comment will be deleted (as it's automated - part of the closing the question as duplicate) when question will be closed ... and according [this](https://stackoverflow.blog/2019/11/25/introducing-the-loop-a-foundation-in-listening/) "unwelcomming commiunity" is worst than "quesion quality" which is not true as most of the question can be avoided by doing some research – Selvin Nov 26 '19 at 15:15
  • What is the point of all this Duplicate and down votes? I have couple years experience of using Google, I have also Gmail and Google Photos and Android phone. As I have stated in my question that I have tried such a solution, however as being on learning curve I cant get it work in my code. Probably I am adding these lines of code to a wrong place. I have been checking provided question like 4 times already without result. Thats why I have created my question now. This is not the only solution, I can drop you at least 3 more but I cant get them function correct. – 10101 Nov 26 '19 at 15:20

2 Answers2

3

Have you tried adding:

var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
  • 1
    Thank you for this! Maybe you can guide me where this should be located in my code to function correctly – 10101 Nov 26 '19 at 16:16
0

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);
           }
        }

    }
}
10101
  • 2,232
  • 3
  • 26
  • 66