Looping through an List to select an Element in Selenium

Posted by ChrisMcLellan on Stack Overflow See other posts from Stack Overflow or by ChrisMcLellan
Published on 2014-06-13T15:01:10Z Indexed on 2014/06/13 15:24 UTC
Read the original article Hit count: 258

I'm attempting to write a Page Class for Links within the Header of the website I'm testing. I have the following link structure below

<ul>

    <li><a href="/" title="Home">Home</a></li>
    <li><a href="/AboutUs" title="About Us">About Us</a> </li>
    <li><a href="/Account" title="Account">Account</a></li>
    <li><a href="/Account/Orders" title="Orders">Orders</a></li>
    <li><a href="/AdministrationPortal" title="Administration Portal">Administration Portal</a></li>
</ul>

What I want to do is store these into a List, then when a user select one of the links, it will take then to the page they should go to.

I have started with the following code below

        List<IWebElement> headerElements = new List<IWebElement>();
        headerElements.Add(WebDriver.FindElement(By.LinkText("Home")));
        headerElements.Add(WebDriver.FindElement(By.LinkText("About Us")));
        headerElements.Add(WebDriver.FindElement(By.LinkText("Account")));
        headerElements.Add(WebDriver.FindElement(By.LinkText("Orders")));
        headerElements.Add(WebDriver.FindElement(By.LinkText("Administration Portal")));
        headerElements.Add(WebDriver.FindElement(By.LinkText("Log in / Register")));
        headerElements.Add(WebDriver.FindElement(By.LinkText("Log off")));

I was thinking for using a for loop to do this, would this be the best way. I'm trying to avoid writting methods like the one below for each link

  public void SelectCreateNewReferralLink()
    {
        var selectAboutUsLink = ( new WebDriverWait(WebDriver, new TimeSpan(50))).Until
            (ExpectedConditions.ElementExists(By.CssSelector("#main > a:nth-of-type(1)")));
        selectCreateNewReferralLink.Click();
    }

I'm using C#, with WebDriver attempting to write this

Any Help would be great

Thanks

Chris

© Stack Overflow or respective owner

Related posts about c#

Related posts about selenium