1

So I am trying to select an element that has an Id of "edit-". The problem is the element is dynamic. So there could be nothing, 5 different ones, or one. In each case the number following the dash changes based on what edit button I want to select. I am not sure how to "choose" what one I want in a method.

Currently the code I have is

private ReadOnlyCollection<IWebElement> referenceEditButtons => driver.FindElements(By.Id("edit-0"));

public void EditReference(int reference)
    {
        this.referenceEditButtons[reference].Click();
    }

The problem is there technically is only 1 instance of edit-0 on the webpage. I would have to make a variable for each edit button which I want to see if I can stay away from since it would add a bunch of variables and methods. So is there a way to simplify this to make the "referenceEditButtons" variable be able to select any version of "edit-" that I would need it to or something different?

tmcdaniel
  • 25
  • 3

1 Answers1

0

To select each available edit button with the value of id attribute starting with edit- you can use either of the following Locator Strategies:

  • CssSelector:

    private ReadOnlyCollection<IWebElement> referenceEditButtons => driver.FindElements(By.XPath("[id^='edit-']"));
    
  • XPath:

    private ReadOnlyCollection<IWebElement> referenceEditButtons => driver.FindElements(By.XPath("//*[starts-with(@id, 'edit-')]"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Just to add further complexity lol technically if our page has an Applicant and a CoApplicant then there could be 2 "edit-0" one located under the applicant and one located under the coapplicant. How would I differentiate those? – tmcdaniel Jan 11 '22 at 20:38
  • @tmcdaniel Locate the parent of Applicant/CoApplicant with it's unique attributes and then reach to it's descendant with the [Locator Strategies](https://stackoverflow.com/questions/48054321/of-the-many-findelements-by-functions-in-selenium-when-would-you-use-one-over/48056120#48056120) – undetected Selenium Jan 11 '22 at 20:42
  • 1
    Thank you! I am still learning how to solve some of these more complex problems so I appreciate the help/info. – tmcdaniel Jan 11 '22 at 20:45
  • I have one more question that idk if you have the time to answer or if you have experience with PageObject modeling but I have elements defined but I don't know if I can make any of the private ReadOnlyCollections also follow the format I am doing. I have IWebElements defined like this: [FindsBy(How = How.Id, Using = "WhateverId")] private IWebElement elementVariableName Do you know if I can create a collection in the same format? Or is the ReadOnlyCollection the only way to get all the elements? – tmcdaniel Jan 11 '22 at 20:51
  • @tmcdaniel Raise a new question with all the details. – undetected Selenium Jan 11 '22 at 20:54
  • 1
    https://stackoverflow.com/questions/70673440/finding-multiple-elements-with-pageobject-modeling Here is the new question if you have a moment :) – tmcdaniel Jan 11 '22 at 21:05
  • @tmcdaniel umm, saw it, gimme a moment. – undetected Selenium Jan 11 '22 at 21:06