get href value (WebDriver)

后端 未结 2 939
囚心锁ツ
囚心锁ツ 2021-02-05 10:16

How do I get a value from href?

like this eg:

Test
相关标签:
2条回答
  • 2021-02-05 10:57

    You have pointed your element to 'div' instead of 'a'

    Try the below code

    driverCE.findElement(By.xpath("//div[@id='testId']/a")).getAttribute("href");
    
    0 讨论(0)
  • 2021-02-05 11:05

    If you got more than one anchor tag, the following code snippet will help to find all the links pointed by href

    //find all anchor tags in the page
    List<WebElement> refList = driver.findElements(By.tagName("a"));
    
       //iterate over web elements and use the getAttribute method to 
       //find the hypertext reference's value.
    
        for(WebElement we : refList) {
            System.out.println(we.getAttribute("href"));
        }
    
    0 讨论(0)
提交回复
热议问题