how to get attribute value using selenium and css

混江龙づ霸主 提交于 2019-11-29 10:46:52

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");
    

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");
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!