Javascript 如何使用selenium获取链接href?

Javascript 如何使用selenium获取链接href?,javascript,selenium,selenium-webdriver,Javascript,Selenium,Selenium Webdriver,这是im自动化页面上的链接: 这是当我检查它的元素时: 我只想得到 /控制台/代理发票/2297294 哪个是href 我该怎么做 谢谢 您可以使用getAttribute方法: String hrefText = yourLinkElement.getAttribute("href"); 如果您还需要首先找到linkElement,则需要提供更多代码 假设您只有一个链接元素: WebElement yourLinkElement = driver.findElement(By.tagNa

这是im自动化页面上的链接:

这是当我检查它的元素时:

我只想得到

/控制台/代理发票/2297294

哪个是href

我该怎么做


谢谢

您可以使用
getAttribute
方法:

String hrefText = yourLinkElement.getAttribute("href");
如果您还需要首先找到linkElement,则需要提供更多代码

假设您只有一个链接元素:

WebElement yourLinkElement = driver.findElement(By.tagName("a));
或者,例如,如果您已经识别了div元素,我们在您的问题中看到了结束标记,那么您可以执行以下操作:

WebElement yourLinkElement = divElement.findElement(By.tagName("a));

首先查找元素并在变量中获取其属性值,如下所述

String linkText = webDriver.findElement(By.tagName("a")).getAttribute("href");