简体   繁体   中英

Selenium XPath - select element by text which is after comment

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?

You can use:

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

For more info see this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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