简体   繁体   English

如何使用硒获取链接href?

[英]How to get a link href using selenium?

this is a link on a page im automating: 这是页面上的链接,我正在自动执行:

在此处输入图片说明

and this is when I inspect it's element: 这是当我检查它的元素时:

在此处输入图片说明

I want to get only the 我只想得到

/console/agent_invoices/2297294 /控制台/ agent_invoices / 2297294

which is the href... 这是href ...

How can I do this? 我怎样才能做到这一点?

thanks! 谢谢!

You can use the getAttribute method: 您可以使用getAttribute方法:

String hrefText = yourLinkElement.getAttribute("href");

If you also need to find your linkElement first, you need to provide more code. 如果还需要先找到linkElement,则需要提供更多代码。

Given you only have one link Element: 假设您只有一个链接元素:

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

Or for example if you already identified the div-Element, that we see with the closing tag in your question you can do this: 或者例如,如果您已经标识了div-Element,我们可以在问题中使用结束标记看到该div-Element,则可以执行以下操作:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM