3

I have an HTML like this:

"<div **id="xx-abc-list-1"**>
<button ng-reflect-class-base="icon component" **id="xx-abc-list-button-2"**><span class="mat-button-wrapper">
</div>

<div **id="xx-abc-list-2"**>
<button ng-reflect-class-base="icon component" **id="xx-abc-list-button-2"**><span class="mat-button-wrapper">
</div>"

Now, i wanted to get the list of all the elements ending with id = xx-abc-list- only so i used the below code.

@FindBy(xpath = "//*[contains(@id,'xx-abc-list-')]");
List<WebElement>;

But this gives me 4 count considering other elements (xx-abc-list-button-) as well.

What should i modify in my above code to get just 2 values that i need?

JeffC
  • 22,180
  • 5
  • 32
  • 55
Lucky
  • 33
  • 4

1 Answers1

4
@FindBy(xpath = "//*[contains(@id,'xx-abc-list-')]");

The * in your XPath above indicates any element (it's a wildcard). In your case, you want only DIVs so you can change that to

@FindBy(xpath = "//div[contains(@id,'xx-abc-list-')]");

and it should get you what you want.

In a more complicated scenario where you want two but not the four and all are DIVs, you can still use a "not contains()" to exclude the IDs that contain "-button-".

@FindBy(xpath = "//*[contains(@id,'xx-abc-list-')][not(contains(@id,'-button-'))]")

How to use not contains() in xpath?

Lucky
  • 33
  • 4
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • 1
    Great. If this, or any other answer, was useful please upvote it. Once you find the answer to your question, please mark it as accepted so the question isn't left unanswered. – JeffC Jun 04 '18 at 23:43