I need to get the URL from the element "name" in Selenium C#

Viewed 37

I have tried

 private string GetId(int index)
        {
            var xPath = "xpath";
            var name = driver.FindElements(By.XPath(xPath)).GetAttribute("href");

            return name[0].Text;
        }

Error

... does not contain a definition for 'GetAttribute' and no accessible extension method 'GetAttribute' accepting a first argument of type 'ReadOnlyCollection' could be found (are you missing a using directive or an assembly reference?...

1 Answers

"driver.FindElements()" returns an array of found elements, so if you want to read the "href" attribute of the first found element, you should use this code:

var name = driver.FindElements(By.XPath(xPath));

return name[0].GetAttribute("href");
Related