2011-11-30 45 views
3

我是Selenium Webdriver中的新成员。当我使用这段代码时,我得到了输出和警告。请帮助我,我怎样才能忽略这个警告。我的代码是:Selenium Webdriver警告 - 无效令牌“屏幕”

package com.webdriver.Webdriver; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 

public class Example { 

    public static void main(String[] args) { 
     // Create a new instance of the html unit driver 
     // Notice that the remainder of the code relies on the interface, 
     // not the implementation. 
     WebDriver driver = new HtmlUnitDriver(); 
     // And now use this to visit Google 
     driver.get("https://mobile.twitter.com/andres/about"); 
     // Find the text input element by its name 
     //WebElement element = driver.findElement(By.xpath("//title")); 
     WebElement element = driver.findElement(By.xpath("//div[@class='footer']/strong/a")); 
     String s=element.getText(); 
     // Enter something to search for 
     //element.sendKeys("Cheese!"); 
     // Now submit the form. WebDriver will find the form for us from the element 
     //element.submit(); 
     // Check the title of the page 
     System.out.println("Page title is: " + driver.getTitle()); 
     System.out.println(s); 
     driver.quit(); 
     } 

} 

警告:

Nov 30, 2011 2:54:23 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error

WARNING: CSS error: [1:1724] Error in @media rule. Invalid token "screen". Was expecting one of: , , .

Nov 30, 2011 2:54:23 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning

WARNING: CSS warning: [1:1724] Ignoring the whole rule.

Nov 30, 2011 2:54:23 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error

WARNING: CSS error: [1:1908] Error in @media rule. Invalid token "screen". Was expecting one of: , , .

Nov 30, 2011 2:54:23 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning

WARNING: CSS warning: [1:1908] Ignoring the whole rule.

Nov 30, 2011 2:54:23 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error

WARNING: CSS error: [1:3437] Error in @media rule. Invalid token "screen". Was expecting one of: , , .

Nov 30, 2011 2:54:23 PM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler warning

WARNING: CSS warning: [1:3437] Ignoring the whole rule.

Page title is: Twitter Refresh now

感谢

回答

1

https://mobile.twitter.com/andres/about页面的CSS文件中包含意外/不支持/断码。

您有两种选择:用SilentCssErrorHandler替换DefaultCssErrorHandler或将其替换为仅过滤这些特定错误的默认实现(将错误消息与输出中显示的字符串相匹配)。

要安装自定义处理程序:

HtmlUnitDriver driver = new HtmlUnitDriver(); // Can't use generic driver anymore 
driver.getWebClient().setCssErrorHandler(handler); // install your handler 
+0

'getWebClient()'不是公共方法。 – Bombe

+0

上面的代码是从我的项目复制并粘贴的。如果该方法不再公开,则可以获取源代码并将其公开,或者使用反射来调用它。 *叹气*当单元测试库与你争吵时,你不也讨厌它吗? –

5

您需要实现自己的HtmlUnitDriver子类,然后设置cssErrorHandlerSilentCssErrorHandler实例。

这可以很容易地用内部类实现,重写构造并设置错误处理程序:

import com.gargoylesoftware.htmlunit.SilentCssErrorHandler; 
import org.openqa.selenium.htmlunit.HtmlUnitDriver; 

public abstract class MyBaseTestCase { 
    protected HtmlUnitDriver webDriver = new SilentHtmlUnitDriver(); 

    protected class SilentHtmlUnitDriver extends HtmlUnitDriver { 
     SilentHtmlUnitDriver() { 
      super(); 
      this.getWebClient().setCssErrorHandler(new SilentCssErrorHandler()); 
     } 
    } 
} 

然后你可以扩展这个基类,并访问webdriver的对象,并享受免费的垃圾邮件输出!