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

}

警告:

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

谢谢

解决方法:

您需要实现自己的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对象并享受无垃圾邮件输出

相关文章

转载地址:https://www.cnblogs.com/mini-monkey/p/12104821...
web自动化测试过程中页面截图相对比较简单,可以直接使用sel...
目录前言一、Selenium简介二、浏览器驱动1.浏览器驱动参考2....
一、iframe的含义:iframe是HTML中框架的一种形式,在对界面...
转载请注明出处❤️作者:测试蔡坨坨原文链接:caituotuo.to...
'''##**认识selenium**​**下载:pipinstall...