【问题标题】:how to get an attribute value from a href link in selenium如何从硒中的href链接获取属性值
【发布时间】:2020-10-14 10:42:50
【问题描述】:

我正在尝试从“a href”属性中获取链接

<a href="http://fgkzc.downloader.info/download.php?id=bc56585624bbaf29ebdd65d0248cb620" rel="nofollow" class="dl_link 1" style="">Download</a>

我在做什么:

ReadOnlyCollection<IWebElement> lists1 = driver.FindElements(By.ClassName("dl_link"));

string s = lists1[0].GetAttribute("a href");

我正在获取类“dl_link 1”的元素,但我无法获取它的链接,字符串为空?

【问题讨论】:

    标签: c# selenium selenium-webdriver


    【解决方案1】:

    您需要使用实际属性名称调用GetAttribute()。替换:

    lists1[0].GetAttribute("a href");
    

    与:

    lists1[0].GetAttribute("href");
    

    【讨论】:

      【解决方案2】:

      C#

      element.GetAttribute("attribute name");
      

      红宝石

      element.attribute("attribute name")
      

      Python

      element.get_attribute("attribute name")
      

      Java

      element.getAttribute("attribute name")
      

      【讨论】:

        【解决方案3】:

        在 C# 中,用于锚标记

        string url = lists1[0].Url();
        

        将产生与

        相同的结果
        string url = ists1[0].GetAttribute("href");
        

        注意: 即使 href 在锚标记中有相对路径,两者都返回完整的 Url。

        For <a href="/profile/my-profile-id" /> 
        
        element.Url();
        // returns http://mywebsite.com/profile/my-profile-id
        
        element.GetAttribute("href"); 
        // returns http://mywebsite.com/profile/my-profile-id
        

        【讨论】:

          猜你喜欢
          • 2021-12-25
          • 2015-08-30
          • 2011-02-08
          • 2023-03-14
          • 2019-04-14
          • 2011-06-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多