2014-09-02 64 views
1

當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 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(); 

所以,我怎麼就能夠抑制發送到控制檯的警告?

+0

waring和warnung之間的拼寫有所不同 – StackFlowed 2014-09-02 13:48:25

+2

@Aeshang「W​​arnung」是德語的「警告」。 – bl4ckscor3 2014-09-02 13:48:59

回答

0

雖然這些功能的瀏覽器specific.Say 爲Firefox它會像webdriver.log.driver可以使用能力

處理這個問題。有關更多詳細信息,請參閱下面的鏈接。對硒的webdriver記錄

Desired Capabilities

更多細節可以在這裏找到:Logging in Selenium

更新:如果您正在使用HtmlUnitDriver對於沒有瀏覽器中打開,你必須使用你自己的日誌記錄系統具有適當的配置。您可以選擇Log4j或SimpleLog庫。 如果您在使用log4j的,只是加入這一行的文件commons-logging.properties

org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger 

,並保持的log4j.xml在財產文件kept.Normally同一文件夾中/src目錄/主要/資源:文件夾,以防其Maven項目。然後在log4j.xml中,您可以根據需要配置日誌記錄級別。

+0

謝謝,但我使用HtmlUnitDriver,正如您在示例中看到的,我無法找到該驅動程序的記錄器。 (使用FirefoxDriver和webdriver.log.driver可以工作,但當Bot在VPS上時,我無法使用FirefoxDriver)。 – bl4ckscor3 2014-09-02 14:14:54