没有办法做到这一点WebDriver http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.html(从 v2.21.0 开始)。你可以尝试更换普通的findElement(By) http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebDriver.html#findElement%28org.openqa.selenium.By%29一种经过调整的方法,该方法使用 JavaScript 来突出显示找到的元素:
// Draws a red border around the found element. Does not set it back anyhow.
public WebElement findElement(By by) {
WebElement elem = driver.findElement(by);
// draw a border around the found element
if (driver instanceof JavascriptExecutor) {
((JavascriptExecutor)driver).executeScript("arguments[0].style.border='3px solid red'", elem);
}
return elem;
}
现在您已经明白了,有一个改进版本可以恢复原始版本border
当找到并突出显示新元素时,最后一个元素的位置:
// assuming JS is enabled
private JavascriptExecutor js = (JavascriptExecutor)driver;
private WebElement lastElem = null;
private String lastBorder = null;
private static final String SCRIPT_GET_ELEMENT_BORDER;
private static final String SCRIPT_UNHIGHLIGHT_ELEMENT;
void highlightElement(WebElement elem) {
unhighlightLast();
// remember the new element
lastElem = elem;
lastBorder = (String)(js.executeScript(SCRIPT_GET_ELEMENT_BORDER, elem));
}
void unhighlightLast() {
if (lastElem != null) {
try {
// if there already is a highlighted element, unhighlight it
js.executeScript(SCRIPT_UNHIGHLIGHT_ELEMENT, lastElem, lastBorder);
} catch (StaleElementReferenceException ignored) {
// the page got reloaded, the element isn't there
} finally {
// element either restored or wasn't valid, nullify in both cases
lastElem = null;
}
}
}
还有脚本!我使用从文件加载它们FileUtils.readFileToString() http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html#readFileToString%28java.io.File%29.
SCRIPT_GET_ELEMENT_BORDER (IE 友好版本取自本网站 http://www.quirksmode.org/dom/getstyles.html),如果通过更改背景颜色(例如仅更改底部边框)来使用突出显示,则会更短。但这是最好的一个:)。
/*
* Returns all border properties of the specified element as String,
* in order of "width style color" delimited by ';' (semicolon) in the form of:
*
* "2px inset #000000;2px inset #000000;2px inset #000000;2px inset #000000"
* "medium none #ccc;medium none #ccc;1px solid #e5e5e5;medium none #ccc"
* etc.
*/
var elem = arguments[0];
if (elem.currentStyle) {
// Branch for IE 6,7,8. No idea how this works on IE9, but the script
// should take care of it.
var style = elem.currentStyle;
var border = style['borderTopWidth']
+ ' ' + style['borderTopStyle']
+ ' ' + style['borderTopColor']
+ ';' + style['borderRightWidth']
+ ' ' + style['borderRightStyle']
+ ' ' + style['borderRightColor']
+ ';' + style['borderBottomWidth']
+ ' ' + style['borderBottomStyle']
+ ' ' + style['borderBottomColor']
+ ';' + style['borderLeftWidth']
+ ' ' + style['borderLeftStyle']
+ ' ' + style['borderLeftColor'];
} else if (window.getComputedStyle) {
// Branch for FF, Chrome, Opera
var style = document.defaultView.getComputedStyle(elem);
var border = style.getPropertyValue('border-top-width')
+ ' ' + style.getPropertyValue('border-top-style')
+ ' ' + style.getPropertyValue('border-top-color')
+ ';' + style.getPropertyValue('border-right-width')
+ ' ' + style.getPropertyValue('border-right-style')
+ ' ' + style.getPropertyValue('border-right-color')
+ ';' + style.getPropertyValue('border-bottom-width')
+ ' ' + style.getPropertyValue('border-bottom-style')
+ ' ' + style.getPropertyValue('border-bottom-color')
+ ';' + style.getPropertyValue('border-left-width')
+ ' ' + style.getPropertyValue('border-left-style')
+ ' ' + style.getPropertyValue('border-left-color');
}
// highlight the element
elem.style.border = '2px solid red';
return border;
SCRIPT_UNHIGHLIGHT_ELEMENT
var elem = arguments[0];
var borders = arguments[1].split(';');
elem.style.borderTop = borders[0];
elem.style.borderRight = borders[1];
elem.style.borderBottom = borders[2];
elem.style.borderLeft = borders[3];
欢迎任何问题、注释、请求和改进!