简体   繁体   中英

Why's my XPath String-length not working? (Java)

WebElement sendBtn = driver.findElement(By.xpath(
    "//*[contains(.,'About') and contains(.,'/') and (string-length(text()) < 9)]"
));

I am trying to get it to select an element that contains the word "about" the letter "/" and the string length of less than 9 but it's doesn't select the right elements.

contains() applies to all text descendants, so if you don't specify the element name, you might get all ancestors of the nodes that actually contain the specified string.

To only apply the predicates to the actual texts in the elements, replace . with text() :

//*[ contains(text(), 'About')
     and contains(text(), '/')
     and string-length(text()) < 9
   ]

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