[英]How to get the text from <span> tag and use this text on other page using Selenium and Java
[英]Selenium (Java) Get FULL text from shortened displayed on page
我想得到鏈接的完整文本,它包含在頁面元素中,並且可能使用.getText()
參數檢索值。 問題是,在頁面上只有部分顯示后跟“...”。
.getText returns = "http://www.example.com/es_es/spa..."
what I need to get ="http://www.example.com/es_es/spain/legal-notice.html"
這是HTML
<a href="http://www.example.com/es_es/spain/legal-notice.html" target="_blank">http://www.example.com/es_es/spa...</a>
我能夠找到元素,而不是檢索它的全文。 我嘗試了.getAttribute("href")
但它returns "null"
。
謝謝!
使用xpath嘗試這個:
WebElement hrefText=driver.findElement(By.xpath("//a[@href='http://www.example.com/es_es/spain/legal-notice.html']"));
String text_hrefText=hrefText.getAttribute("href");
System.out.println(text_hrefText);
你想使用getText()
方法,但getText()
將是有用的,而我們只想檢索innerHTML
。 但是如果我們使用getAttribute()
方法,我們將能夠提取WebElement
任何attributes
。
現在,要檢索href
屬性的全文,可以使用以下代碼行:
System.out.println(driver.findElement(By.xpath("//a[contains(@href,'example.com/es_es/spain/legal-notice')]")).getAttribute("href"));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.