I am trying to search the text and highlight it, but it give me a error no such element: Unable to locate.

I am using boolean then I found the text but i don't know how to highlight it in a 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();

         System.out.print("Text not Found");

Other thing, I use WebElement but it give me error no such element found

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


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

1 Answers

Dmitri T On

Looking into https://anyaut.com/ website there is the following construction:

<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>

It means that there are 2 text nodes and your expression is taking into consideration only first one, to wit Built using

In order to extract the frequent test steps as KEYWORDS text you need to provide the position predicate like:

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

Or if you want to go for the wildcard you need to amend your expression to look like:

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

enter image description here