在PhantomJS Selenium Webdriver中关闭INFO信息

3

我正在使用 Selenium PhantomJS Webdriver 在我的 C# 应用程序中爬取一个页面。由于我经常使用控制台,因此我希望关闭它不断输出的 INFO 信息。是否有人遇到了同样的问题?

4个回答

1
我认为这个字符串必须添加到Wasp的回答中:
service.HideCommandPromptWindow = true;

所以
    static IWebDriver CreateInfolessPhantom(string logFilePath)
{
    PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
    service.LogFile = logFilePath;
    service.HideCommandPromptWindow = true;
    return new PhantomJSDriver(service);
}

那样可以。这是唯一有效的方法。 另外,你不必指定 service.LogFile(除非你需要一个!),只需设置 service.HideCommandPromptWindow = true; 就可以了。 非常感谢。 :) - BrainSlugs83

0
以下代码对我来说运行良好:
var driverService = PhantomJSDriverService.CreateDefaultService(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));
driverService.SuppressInitialDiagnosticInformation = true;
driverService.AddArgument("--webdriver-loglevel=NONE");
driver = new PhantomJSDriver(driverService);

0

我找到了一个解决方法,虽然不是很优美,但对我很有帮助。它清除了每次启动webdriver时创建的信息。我使用一个简单的方法返回一个新的driver,在其中调用一个方法来清除控制台中创建的行,并将控制台光标定位到下一行。

public static IWebDriver GetDriver()
    {
        IWebDriver driver = new PhantomJSDriver();
        ClearCurrentConsoleLine();
        return driver;
    }
    public static void ClearCurrentConsoleLine()
    {
        int pos = Console.CursorTop;
        for (int i = 0; i < 20; i++)
        {
            Console.SetCursorPosition(0, Console.CursorTop - i);
            Console.Write(new string(' ', Console.WindowWidth));
            Console.SetCursorPosition(0,pos);
        }
        Console.SetCursorPosition(0,Console.CursorTop-19);
    }

-1
这是我的第一个代码提交,如果它对你有帮助,请投票支持它,如果你有改进意见,请添加评论。
using System;
using System.Collections.Generic;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Support;
using OpenQA.Selenium.PhantomJS;

namespace PhantomJSExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string logFilePath = @"C:\Users\Username\Desktop\outputLog.log";
            IWebDriver driver = CreateInfolessPhantom(logFilePath);
        }

        // Prepares a PhantomJS driver that does not dislay its logs in the console window.
        // The trade-off is, you have to give it a file path to output to instead.
        static IWebDriver CreateInfolessPhantom(string logFilePath)
        {
            PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
            service.LogFile = logFilePath;

            return new PhantomJSDriver(service);
        }
    }
}

这个不起作用。——它仍然会在控制台上大量输出信息。 - BrainSlugs83
此外,我建议不要硬编码桌面路径,而是像这样查找:var logFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "output.log");。即使您知道用户的用户名,上面指定的路径有时也可能是错误的;因为用户可以将其桌面文件夹放在任何地方,它并不是固定的。 - BrainSlugs83

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接