Java 试图关闭控制台中的警告,但失败
当YouTube链接发布到频道时,我的IRC机器人会发送视频的统计数据。但是我收到了大量的警告,它们让我很恼火,并且把我的控制台弄得乱七八糟:Java 试图关闭控制台中的警告,但失败,java,selenium,warnings,gargoyle,Java,Selenium,Warnings,Gargoyle,当YouTube链接发布到频道时,我的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
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.
但是没有用。我使用以下代码获取结果:
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。有关更多详细信息,请参阅下面的链接
有关登录Selenium Webdriver的更多详细信息,请参见:
更新:如果您使用的是未打开浏览器的HtmlUnitDriver,则必须使用具有正确配置的自己的日志系统。您可以选择Log4j或SimpleLog库。
如果您使用log4j,只需在commons-logging.properties文件中添加这一行
并将log4j.xml保存在保存属性文件的同一文件夹中。如果是Maven项目,通常为/src/main/resources:文件夹。然后在log4j.xml中,您可以根据需要配置日志记录级别 waring和waring的拼写不同warnung@AeshangWarnung是德语中警告的意思。谢谢,但是我使用的是HtmlUnitDriver,正如您在示例中看到的,我找不到该驱动程序的记录器。使用FirefoxDriver和webdriver.log.driver是可行的,但是当机器人位于VPS上时,我不能使用FirefoxDriver。
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();
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger