有没有办法找到特定的文本并突出显示它

时间:2019-05-09 17:25:29

标签: selenium

我正在尝试搜索文本并将其突出显示,但是它给我一个错误,没有这样的元素:无法找到。

我正在使用布尔值,然后我找到了文本,但是我不知道如何在硒中突出显示它。

myD.getPageSource().contains("Power of Selenium WebDriver with ");
boolean Error = myD.getPageSource().contains("frequent test steps as KEYWORDS");

        if (Error == true)
        {
         System.out.print("Text Found");

        //   highLightElement();

        }
        else
        {
         System.out.print("Text not Found");
        }

其他事情,我使用WebElement,但是它给我错误,找不到这样的元素

    driver.get("https://anyaut.com/");
    WebElement matchedElement=driver.findElement(By.xpath("//*[contains(text(),'frequent test steps as KEYWORDS')]"));
    highLightElement(matchedElement);

HighlightElement:

public static void highLightElement(WebElement element){
    JavascriptExecutor js = (JavascriptExecutor)driver;   
    js.executeScript("arguments[0].setAttribute('style','background: yellow; border: 2px solid red;');", element);   
}

1 个答案:

答案 0 :(得分:0)

查看https://anyaut.com/网站的结构如下:

<p class="green-border-inner-content">Built using <font color="#34d293">Page Object, Data Driven &amp; Keyword Driven
    Automation Frameworks</font>. Has the facility to group frequent test steps as KEYWORDS , along with reusable Test
    Data and Element Identifiers.</p>

这意味着有2个文本nodes,而您的表情只考虑了第一个,即Built using

要提取frequent test steps as KEYWORDS文本,您需要提供position predicate,例如:

//p[contains(text()[2],'frequent test steps as KEYWORDS')]

或者,如果您想使用wildcard,则需要将表情修改为:

//*[text()[contains(.,'frequent test steps as KEYWORDS')]]

enter image description here