繁体   English   中英

如何使用Selenium检查页面上是否突出显示文本?

[英]How to check if a text is highlighted on the page using Selenium?

1>突出显示的文字是什么意思? 假设您浏览任何页面,使用鼠标或其他方法选择该页面上的任何文本。 让我们说“ 你好 “,”注册“文字突出显示。如果错误,请更正我的理解。

2>我尝试了下面的代码来获得颜色和背景颜色的CssValue。 使用给定的代码:

    driver.get("https://facebook.com");
    String textColor = driver.findElement(By.xpath("//*[@id='pageFooter']/div[3]/div/span")).getCssValue("color");
    String bkgColor = driver.findElement(By.xpath("//*[@id='pageFooter']/div[3]/div/span")).getCssValue("background-color");
    System.out.println("TextColor : " + textColor );
    System.out.println("Background Color : " + bkgColor);

输出: -

TextColor : rgba(115, 115, 115, 1)
Background Color : transparent

* TextColor给出文本的颜色。

**为突出显示我使用Robot类发送Ctrl + A,虽然它选择了整个页面,但它应该输出一些不同的值。 在这种情况下,它也只提供上面输出的文本颜色。

如果我做得对,或者我对突出显示的文字的理解是正确的,请告诉我。

您不能单独使用Selenium,但您可以使用Selenium执行javascript。 Js非常简单:

window.getSelection().toString();

在此输入图像描述

所以就:

((JavascriptExecutor)driver).executeScript("return window.getSelection().toString();");

PS将无法在<= IE8中工作(将提供您将需要的方式)

在浏览器中选择文本后,具有所选文本的元素的CSS样式不会更改。 你可以做的是获取当前“活动”元素 ,看看它是否是所需的元素

WebElement desiredElement = driver.findElement(By.xpath("//*[@id='pageFooter']/div[3]/div/span"));
WebElement activeElement = driver.switchTo().activeElement();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM