how to get attribute value using selenium and css

后端 未结 2 578
刺人心
刺人心 2020-12-19 07:26

I have the following HTML code:

 2 
<
相关标签:
2条回答
  • 2020-12-19 08:13

    If your HTML consists solely of that one <a> tag, then this should do it:

    String href = selenium.getAttribute("css=a@href");
    

    You use the DefaultSelenium#getAttribute() method and pass in a CSS locator, an @ symbol, and the name of the attribute you want to fetch. In this case, you select the a and get its @href.

    In response to your comment/edit:

    1. The part after @ tells Selenium that that part is the name of the attribute.

    2. You should place :contains('2') before @href because it's part of the locator, not the attribute. So, like this:

      selenium.getAttribute("css=a:contains('2')@href");
      
    0 讨论(0)
  • 2020-12-19 08:21

    Changing css=a@href to href should do the trick. Let me if this did not work.

        List<WebElement> ele = driver.findElements(By.className("c"));
            for(WebElement e : ele)
            {
               String doctorname = e.getText();
               String linkValue = e.getAttribute("href");
            }
    
    0 讨论(0)
提交回复
热议问题