获取href值(WebDriver)

如何从href获取值?

像这样:

Test

我需要将该值作为字符串。

我试过这个:

 String e = driverCE.findElement(By.xpath("//div[@id='testId']")).getAttribute("href"); JOptionPane.showMessageDialog(null, e); 

但只返回NULL值…

你已经将你的元素指向’div’而不是’a’

请尝试以下代码

 driverCE.findElement(By.xpath("//div[@id='testId']/a")).getAttribute("href"); 

如果您有多个锚标记,则以下代码段将有助于查找href指向的所有链接

 //find all anchor tags in the page List 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")); }