【问题标题】:I need to get the URL from the element "name" in Selenium C#我需要从 Selenium C# 中的元素“名称”获取 URL
【发布时间】:2021-10-07 03:42:52
【问题描述】:

我试过了

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

            return name[0].Text;
        }

错误

...不包含“GetAttribute”的定义,并且找不到接受“ReadOnlyCollection”类型的第一个参数的可访问扩展方法“GetAttribute”(您是否缺少 using 指令或程序集引用?...

【问题讨论】:

  • FindElements return's a ReadOnlyCollection<WebElement> 并且您尝试在该集合上调用 GetAttribute,因此您遇到错误的原因。你想要所有href 属性吗?您能否更新您的帖子以包含更多详细信息、示例和预期输出?

标签: c# selenium-webdriver


【解决方案1】:

“driver.FindElements()”返回一个找到的元素数组,所以如果你想读取第一个找到的元素的“href”属性,你应该使用这个代码:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 2018-07-13
    • 1970-01-01
    • 2018-12-06
    • 2016-03-29
    • 1970-01-01
    • 2010-10-05
    相关资源
    最近更新 更多