栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在运行时突出显示WebDriver中的元素

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在运行时突出显示WebDriver中的元素

WebDriver
(自v2.21.0起)无法执行此操作。您可以尝试将通常的
findElement(By)
方法替换为经过调整的方法,该方法使用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 enabledprivate 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()

script_GET_ELEMENT_BORDER(从此站点获取的IE友好版本),如果它通过更改背景颜色(例如仅是底部边框)使用突出显示功能,则会更短。但这是最好的一个:)。

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 elementelem.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];

欢迎任何问题,注释,要求和改进!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/470965.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号