Selenium XPath - select element by text which is after comment

KunLun :

I have this HTML:

<div>
    <!-- \/ this div -->
    <div>
        aaa
        <!-- -->
        bbb
    </div>
</div>

I want to select the second div like this: "//div[contains(text(), 'bbb')]", but selenium don't find it.

I tested this:

System.out.println(
    driver.findElement(By.xpath("//div[contains(text(), 'aaa')]")).getText()
); //aaa bbb

System.out.println(
    driver.findElement(By.xpath("//div[contains(text(), 'bbb')]")).getText()
); //Unable to locate element:...

Why don't find element if I looking for text which is after a comment?

Moshe Slavin :

You can use:

System.out.println(
    driver.findElement(By.xpath("//div[contains(., 'bbb')]")).getText()
);

For more info see this answer

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165027&siteId=1