-1

I have a long list of elements I would like to access. They all have a similar id with a index from 1 to 47. Is there a good way to access these as an array or list, as i would like to be able to iterate through them, instead of doing this for each single one

[FindsBy(How = How.Id, Using = "ElementId1")]
public IWebElement Element1;
[FindsBy(How = How.Id, Using = "ElementId2")]
public IWebElement Element2;
[FindsBy(How = How.Id, Using = "ElementId....")]
public IWebElement Element....;

and then making a list of the elements?

Can I somehow make a loop that finds them and puts them into an array itself so I don't need manually define each one?

Jaco9307
  • 13
  • 7
  • This is generally not a good practice because if you create an array of elements, how do you know which one is which? e.g. How do you know Last Name is index 11 and email is index 24? You generally don't unless you memorize them. Copy/pasting 47 declarations and then giving each a readable/understandable name and updating the ID is a minor amount of work vs trying to debug an array of values every time you run into an issue. – JeffC Feb 13 '20 at 15:20
  • @JeffC Well, i dont really care which element is which, they should all be checked for the same conditions. It also varies how many there are so doing it manually causes a bunch elements to be null. – Jaco9307 Feb 14 '20 at 12:25
  • OK... so one fails... which one failed? Which one was missing? Now you see the issue? Sounds like you need to better understand how the page works so you can better test the elements when they are expected to be there. – JeffC Feb 14 '20 at 14:35
  • As i just said, i dont care which element is which, that includes which one fails. Sounds like you think im an idiot or something. I know what i need, and I dont need you, with your limited knowledge of the task I am working on, to come here and tell me how to do it. Suggestions are fine but i clearly speed out I know the issues with it and that they dont affect me – Jaco9307 Feb 27 '20 at 09:56

2 Answers2

1

You can look for elements that start with id="ElementId"

[FindsBy(How = How.CssSelector, Using = "[id^='ElementId']")]
public List<IWebElement> Elements;

If the number is in the middle you can search for elements with id that starts with "Element" and ends with "Id"

[FindsBy(How = How.CssSelector, Using = "[id^='Element'][id$='Id']")]
public List<IWebElement> Elements;
JeffC
  • 22,180
  • 5
  • 32
  • 55
Guy
  • 46,488
  • 10
  • 44
  • 88
1

You Can use Xpath with contains id attribute

 @FindBy(how= How.XPATH, using= "//*[contains(@id, 'ElementId')]")
    private List<WebElement> allElements;

OR

CSS with starts with using ^

@FindBy(how= How.CSS, using= "[id^='ElementId']")
private List<WebElement> allElements;

OR

Both using @FindAll annotation

@FindAll({@FindBy(how= How.CSS, using= "[id^='ElementId']"),
        @FindBy(how= How.XPATH, using= "//*[contains(@id, 'ElementId')]")})
private List<WebElement> allElements;
Muzzamil
  • 2,823
  • 2
  • 11
  • 23
  • If i have a full xpath like /html/body/div[1]/table/tbody/tr[INDEX]/td[4] how would i go about finding all the matching elements not knowing the index? – Jaco9307 Feb 14 '20 at 12:30
  • Now exactly I don't know your requirement so I can't update my solution. but with **@FindAll** annotation you have multiple option to try if selenium find any match with any options it will return a list of element. – Muzzamil Feb 14 '20 at 13:15
  • **//table/tbody/tr//td//*(@commom-attribute ='xyz')** will fetch all matching element you have to find common attribute with same value in all elements. In above solution if **ElementId** is common then it will work with xpath **//*[contains(@id, 'ElementId')]** – Muzzamil Feb 14 '20 at 15:02