如何使用 Selenium 获取特定类中 href 的内容?
我们可以使用 Selenium webdriver 获取特定目标类中的 href 内容。首先,我们必须使用诸如 xpath、css 或 classname 等定位器,定位具有特定类属性值的带有 anchor 标签的元素。
然后,我们必须借助getAttribute方法,并将 href作为参数传递给该方法。让我们看一个带有具有 class 和 href 属性的 anchor 标签的元素的 html 代码。该元素的 href 值应该是 /account/register?hl=en。

示例
代码实现。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class HrefVal{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://secure.indeed.com/account/login");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
// identify element with specific class value
WebElement m= driver.findElement(By.className("login-registrationBoldLink"));
// getAttribute() to href value
String val = m.getAttribute("href");
System.out.println("Href value of targeted: "+ val);
driver.close()
}
}输出

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP