2015-10-07 102 views
1

這是一個網頁IM自動化鏈接:如何使用硒獲得鏈接href?

enter image description here

,這是當我檢查它的元素:

enter image description here

我想只有

/console/agent_invoices/2297294

這是href ...

我該怎麼做?

謝謝!

回答

2

可以使用getAttribute方法:

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

如果您還需要先找到你linkElement,您需要提供更多的代碼。

鑑於你只有一個鏈接元素:

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

或者例如,如果你已經確定的DIV-元素,我們在你的問題的結束標記,你可以做到這一點,請參閱:

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

首先找到元素並在變量中獲取其屬性值,如下所述。

String linkText = webDriver.findElement(By.tagName("a")).getAttribute("href"); 
相關問題