Selenium Webdriver警告 - 无效令牌“屏幕”

时间:2011-11-30 09:08:48

标签: java 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();
        }

}

警告:

  

2011年11月30日下午2:54:23 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler错误

     

警告:CSS错误:[1:1724] @media规则出错。令牌“屏幕”无效。期待以下之一:,。,

     

2011年11月30日下午2:54:23 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler警告

     

警告:CSS警告:[1:1724]忽略整个规则。

     

2011年11月30日下午2:54:23 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler错误

     

警告:CSS错误:[1:1908] @media规则出错。令牌“屏幕”无效。期待以下之一:,。,

     

2011年11月30日下午2:54:23 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler警告

     

警告:CSS警告:[1:1908]忽略整个规则。

     

2011年11月30日下午2:54:23 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler错误

     

警告:CSS错误:[1:3437] @media规则出错。令牌“屏幕”无效。期待以下之一:,。,

     

2011年11月30日下午2:54:23 com.gargoylesoftware.htmlunit.DefaultCssErrorHandler警告

     

警告:CSS警告:[1:3437]忽略整个规则。

     

页面标题是:Twitter   立即刷新

由于

2 个答案:

答案 0 :(得分:5)

您需要实现自己的HtmlUnitDriver子类,然后将cssErrorHandler设置为SilentCssErrorHandler个实例。

这可以通过内部类轻松完成,覆盖构造函数并设置错误处理程序:

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对象并享受无垃圾邮件的输出!

答案 1 :(得分:1)

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

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

安装自定义处理程序:

HtmlUnitDriver driver = new HtmlUnitDriver(); // Can't use generic driver anymore
driver.getWebClient().setCssErrorHandler(handler); // install your handler
相关问题