java - 尝试关闭控制台中的警告但失败

标签 java selenium warnings gargoyle

当 YouTube 链接发布到 channel 时,我的 IRC 机器人会发送视频的统计信息。但我收到了大量警告,它们让我很恼火,而且让我的控制台变得困惑:

Sep 01, 2014 6:09:29 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNUNG: CSS error: 'https://s.ytimg.com/yts/cssbin/www-core-vfl0pJopz.css' [1:41191] Fehler in Style-Regel. (Ungültiger Token "*". Erwartet wurde einer von: <EOF>, <S>, <IDENT>, "}", ";".)
Sep 01, 2014 6:09:29 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning
WARNUNG: CSS warning: 'https://s.ytimg.com/yts/cssbin/www-core-vfl0pJopz.css' [1:41191] Ignoriere die folgenden Deklarationen in dieser Regel.

我想关闭它们,并尝试将此代码添加到我的主要方法中:

java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);

但是没有效果。我正在使用Selenium使用此代码获取结果:

String[] args = Utilities.toArgs(event.getMessage()); //this is the message sent, split by a space
        String link = null;
        boolean shortLink = false;
        WebDriver driver = new HtmlUnitDriver();
        String title = "No value";
        String duration = "No value";
        String views = "No value";
        String likes = "No value";
        String dislikes = "No value";
        String date = "No value";
        String uploader = "No value";

        for(String s : args)
        {
            if(s.contains("www.youtube.com/watch"))
            {
                if(s.contains("v="))
                    link = "http://www.youtube.com/watch?v=" + s.split("v=")[1].substring(0, 11) + "/";
                else
                {
                    Utilities.chanMsg(event, "Couldn't find video id!"); //just sending a message to the channel
                    break;
                }
            }
            else if(s.contains("http://youtu.be/"))
            {
                link = s;
                shortLink = true;
                break;
            }
        }

        if(shortLink)
        {
            String videoId = link.split("/")[3];

            link = "www.youtube.com/watch?v=" + videoId;
        }

        //if someone posts the link without a space between the link and the word before it
        if(!link.startsWith("w"))
            link = link.split(":")[1].substring(2);

        //check that the link is really the link needed (main use is when someone posts a word directly after the link without a space inbetween)
        if(link.length() != 35)
        {
            StringBuilder builder = new StringBuilder();
            builder.append(link);
            builder.delete(35, link.length());
            link = builder.toString();
        }

        //make sure that the links starts with "http://"
        if(!link.startsWith("http://"))
            link = "http://" + link;

        driver.get(link);

        try
        {
            title = driver.findElement(By.xpath("//meta[@itemprop='name']")).getAttribute("content");
        }
        catch(NoSuchElementException e){}

        try
        {
            duration = resolveDuration(driver);
        }
        catch(NoSuchElementException e){}

        try
        {
            views = driver.findElement(By.xpath("//div[@class='watch-view-count']")).getText();
        }
        catch(NoSuchElementException e)
        {
            views = driver.findElement(By.xpath("//span[@class='watch-view-count yt-uix-hovercard-target']")).getText().split("Views")[0];
        }

        try
        {
            likes = driver.findElement(By.xpath("//button[@id='watch-like']/span[@class='yt-uix-button-content']")).getText();
        }
        catch(NoSuchElementException e){}

        try
        {
            dislikes = driver.findElement(By.xpath("//button[@id='watch-dislike']/span[@class='yt-uix-button-content']")).getText();
        }
        catch(NoSuchElementException e){}

        try
        {
            date = driver.findElement(By.xpath("//p[@id='watch-uploader-info']/strong")).getText().split("on")[1];
        }
        catch(NoSuchElementException e){}

        try
        {
            uploader = driver.findElement(By.xpath("//div[@class='yt-user-info']/a")).getText();
        }
        catch(NoSuchElementException e){}

        driver.close();

那么我怎样才能抑制发送到控制台的警告呢?

最佳答案

您可以使用功能来处理此问题

尽管这些功能是特定于浏览器的。对于 firefox 来说,它就像 webdriver.log.driver。欲了解更多详情,您可以引用下面的链接。

Desired Capabilities

有关登录 Selenium Webdriver 的更多详细信息可以在这里找到:Logging in Selenium

更新:如果您使用的 HtmlUnitDriver 没有打开浏览器,则必须使用自己的日志记录系统并进行正确的配置。您可以选择 Log4j 或 SimpleLog 库。 如果您使用 log4j,只需在文件 commons-logging.properties

中添加此行
 org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger

并将 log4j.xml 保存在保存属性文件的同一文件夹中。通常是 /src/main/resources: 文件夹,以防它是 Maven 项目。然后在 log4j.xml 中您可以根据您的需要配置您的日志记录级别。

关于java - 尝试关闭控制台中的警告但失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25624845/

相关文章:

Java 集合 : which collection to use and when?

JAVA:当我尝试访问另一个类文件时,为什么总是收到错误:找不到符号?

java - 组件事件是如何被组件监听器捕获的

java类型安全:not parameterized generic type effect on performance

java - 为什么在runnable接口(interface)中运行用abstract关键字定义的方法

java - Selenium webdriver - 选项卡控件

selenium - PHPUnit Selenium captureScreenshotOnFailure 不起作用?

javascript - Selebiun 爬虫超时问题 C#

maven-2 - 出现 "CHECKSUM FAILED"警告时如何让 Maven 部署失败

如果用户单击后退按钮,则快速添加操作