Bak*_*uni 3 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();
}
}
Run Code Online (Sandbox Code Playgroud)
警告:
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立即刷新
谢谢
您需要实现自己的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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以扩展此基类,并访问webDriver对象并享受无垃圾邮件输出!
| 归档时间: |
|
| 查看次数: |
3158 次 |
| 最近记录: |