1

While trying to figure out how to locate a specific group of web elements, I came across this question and I was wondering if I can do the same action just that it would give me only the descendants elements that contain a specific attribute.

For clarification, I know that I might be able to filter it but I'm looking for a way to do it without filtering.

Furthermore, I'm using java, and as web driver I use chrome driver (and I use Selenium for that, I don't know if it's obvious or not as that's the only web controlling tool that I learned so far...)

Github link

Thanks for the answers ahead!

DGStar
  • 17
  • 7
  • Did this answer help: https://stackoverflow.com/questions/103325/what-is-the-correct-xpath-for-choosing-attributes-that-contain-foo – Andrey Kotov Jun 28 '22 at 15:27
  • @AndreyKotov I'm still learning so I didn't understood both the answer and question there... – DGStar Jun 28 '22 at 15:53
  • ok, then I'll add an answer for you – Andrey Kotov Jun 28 '22 at 15:54
  • Hi, I tried to apply your answer here: sharetext.me/vbfigr5v7z but 'profiles' always end up as a null even when there is profiles with that attribute under that ancestor... Am I doing something wrong? @AndreyKotov – DGStar Jun 28 '22 at 16:49
  • It is a bit hard to help without knowing website's structure. Please repost code with the url. – Andrey Kotov Jun 28 '22 at 17:11
  • @AndreyKotov I added an link to a github repository with the code – DGStar Jun 28 '22 at 19:37
  • you don't need actually that variable called `usersAncestor`, you can find anything using direct `xpath`. See my changes. I commented out `usersAncestor` and initialized `profiles = new ArrayList<>()` - otherwise you'll get `nullPointerException`. On my screenshot you can see that now `elements` list is filled with `WebElement` value (avatar image of user Brad). Code I've changed is here - https://pastebin.com/ZNY0X8uX. Screenshot (save and enlarge it)- https://i.imgur.com/FgduOey.png – Andrey Kotov Jun 28 '22 at 21:07
  • @AndreyKotov If I don't need the ancestor how does it now which element decedents are needed? – DGStar Jun 29 '22 at 09:57
  • In this case webdriver looks for all decedents from the root element (from the `html` tag) - same as if you give it an absolute path to the element. If we take analogy to the file system, you can tell me absolute path to the file `c:/dir1/dir2/dir3/file.txt` OR if you already in `dir2` then you can tell me a relative path that will be `./dir3/file.txt`. So I suggest to use 'absolute XPATHs' everywhere in Selenium, usually it is much simpler. – Andrey Kotov Jun 29 '22 at 10:30

1 Answers1

0

To find elements with attribute that contains 'attributeValue':

List<WebElement> childs = rootWebElement.findElements(By.xpath(".//*[contains(@attributeName,'attributeValue')]"));

To find elements with attribute that fully matches 'attributeValue':

List<WebElement> childs = rootWebElement.findElements(By.xpath(".//*[@attributeName='attributeValue']"));
Andrey Kotov
  • 1,344
  • 2
  • 14
  • 26