【问题标题】:Hide Chrome window while scraping websites抓取网站时隐藏 Chrome 窗口
【发布时间】:2019-11-26 15:01:59
【问题描述】:

我一直在研究网络爬虫。现在一切正常,但我想为我的程序添加最后的润色,并在进程运行时隐藏 Chrome 窗口不可见。

我已尝试将此添加到我的代码中:

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

但是没有成功。有人可以给我一个提示,应该如何添加这个隐藏命令才能正常工作?我应该如何修改我当前的代码来实现后台运行?

这是我的代码:

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

    }
}

【问题讨论】:

  • 你的程序想做什么?您确定使用某种 API 登录不是一个更好的主意吗?
  • 目前正在登录页面,稍后我会在登录后添加文件下载
  • 这能回答你的问题吗? Hide/Silence ChromeDriver window
  • ...这是“隐藏 Chrome 窗口 Selenium”的第一个谷歌搜索结果
  • @Selvin 您可以在发布 cmets 5 分钟后对其进行编辑

标签: c# selenium selenium-webdriver web-scraping web-crawler


【解决方案1】:

您是否尝试过添加:

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

【讨论】:

  • 谢谢你!也许你可以指导我在我的代码中应该在哪里正常运行
【解决方案2】:

好的,经过一番调查,应该是:

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

    }
}

【讨论】:

  • 您不使用@MattWilliams 在他的回答中建议的headless 参数吗?如果有任何 answer 满足您的question,请点击我的answer旁边的空心复选标记Acceptanswer i> 位于 votedown 箭头下方,因此复选标记变为 绿色
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-18
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 2012-09-09
  • 2021-07-27
  • 2014-11-07
相关资源
最近更新 更多