简体   繁体   中英

How to locate element using css selector in selenium webdriver java?

I am trying to locate the text of the third div role:

<div class="jss295">
    <div class="section-header">
        <h3 class="section-header-title">LABELS</h3></div>
    <div role="button" class="jss300 jss299" tabindex="-1"><span class="jss313">system-all</span></div>
    <div role="button" class="jss300 jss299" tabindex="-1"><span class="jss313">paloaltonetworks_iPoiycgPut</span></div>
    <div role="button" class="jss300 jss299" tabindex="-1"><span class="jss313">system-paloaltonetworks</span></div>
</div>

I tried xpath as follows: driver.findElement(By.xpath("//span[contains(text(), '" + str + "')]")).getText() , where str is the text I need.
xpath works but I'd like to use cssSelector instead.

:contains() CSS pseudo-class selector is removed from the CSS3 spec.If you are using the latest browsers, this will not work which confronts to new CSS standards.

If the className is unique you can use cssSelector as:

    span.valueOfClass

//It seems all classNames are same in your scenario.

I guess it is better to use XPath.

Just give a try:

    By.cssSelector(span[text()$=paloaltonetworks])

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