1
        private IWebDriver webDriver;

        [SetUp]
        public void Setup()
        {
            webDriver = new FirefoxDriver();
            webDriver.Navigate().GoToUrl("https://localhost:44311/");
        }

        [Test]
        public void ButtonsCount_IsCorrect_IsTrue()
        {
            var buttons = webDriver.FindElement(...));

            ...
        }

Simple scenario - website contains dynamically created buttons. I want to ensure count of buttons is the same as expected.

Catching it by XPath would be easy, except XPath leads to certain element, here we want to catch multiple dynamically created elements, but each one looks like this:

<a><i></i>Details</a>

The only difference is where href leads, and I have skipped classes because they are not important here.

Conclusions are, it wouls be wise to search for all <a> which contain string Details. Now question is, how to do it in C# driver.

  • 1
    Have you provided enough wait to load page properly? If you still get `list.Count` 0 then check if element present inside an iframe? – KunduK Apr 14 '21 at 15:05
  • @KunduK I think it should be fine because `By.LinkText` works fine. But I still want to know how to solve it if I want to search for `div` or anything else. –  Apr 14 '21 at 18:30

1 Answers1

1

You can use findElements with xpath to find List of elements:

List<WebElement> elementName = driver.FindElements(By.XPath("//a[contains(text(),'Details')]"));
Lia
  • 504
  • 2
  • 7
  • List contains 0 elements, which is not true. –  Apr 14 '21 at 14:47
  • Add wait: `WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5)); IWebElement elementName = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//a[contains(text(),'Details')]"));` – Lia Apr 14 '21 at 15:13
  • What is `ExpectedConditions`? –  Apr 14 '21 at 18:29
  • Please take a look for wait solution here https://stackoverflow.com/questions/6992993/selenium-c-sharp-webdriver-wait-until-element-is-present and https://stackoverflow.com/questions/49866334/c-sharp-selenium-expectedconditions-is-obsolete – Lia Apr 14 '21 at 21:14