TestStack / TestStack.Seleno

Seleno helps you write automated UI tests in the right way by implementing Page Objects and Page Components and by reading from and writing to web pages using strongly typed view models.
http://teststack.github.com/TestStack.Seleno/
MIT License
180 stars 60 forks source link

Not able to find an element using indexes and css selector if there is more than one identical element on the page #214

Closed sirj77 closed 9 years ago

sirj77 commented 9 years ago

Hi, I think, it is possible to find an element by index with css selector (in case if I'm having more than one element with the same name of selector expression), but I'm getting an error even without running the tests. Piece of code:

        public void Test()
        {
            Find.Elements(By.CssSelector(cssSelector))[1];
        }

So, on the test page I have 5 elements with the same selector - cssSelector, but I want to find the second one and do some actions with it or make an assertion. And the visual studio says me that there is an error (it underlines in read the "[1])": "Cannot apply indexing to an expression of type System.Collections.Generic.IEnumerable<OpenQA.Selenium.IWebElement".

What I should change to have this piece of code correct, without errors?

robdmoore commented 9 years ago

Try .ElementAt(1) rather than [1]

On 31 Jul 2015, at 10:35 pm, sirj77 notifications@github.com wrote:

Hi, I think, it is possible to find an element by index with css selector (in case if I'm having more than one element with the same name of selector expression), but I'm getting an error even without running the tests. Piece of code:

    public void Test()
    {
        Find.Elements(By.CssSelector(cssSelector))[1];
    }

So, on the test page I have 5 elements with the same selector - cssSelector, but I want to find the second one and do some actions with it or make an assertion. And the visual studio says me that there is an error (it underlines in read the "[1])": "Cannot apply indexing to an expression of type System.Collections.Generic.IEnumerable<OpenQA.Selenium.IWebElement".

What I should change to have this piece of code correct, without errors?

— Reply to this email directly or view it on GitHub.

sirj77 commented 9 years ago

robdmoore, thanks a lot, it works:

        public bool Test()
        {
            get { return Find.Elements(By.CssSelector(cssSelector)).ElementAt(1).Selected; }
        }