![](/img/trans.png)
[英]How can I get text from element with Selenium webdriver and Java?
[英]How can I get text of an element in Selenium WebDriver?
如何在 Selenium WebDriver 中獲取元素的文本?
我正在嘗試 Text() 方法,但它沒有返回文本。 我也嘗試使用“值”獲取屬性,但它再次不起作用並返回“空”值。
如何獲取所選元素的文本?
讓我們假設您必須在段落中獲取文本,如下所示:
// Initialize browser
ChromeOptions options = new ChromeOptions();
options.setHeadless(false);
// Create a new instance of the Chrome driver
WebDriver driver = new ChromeDriver(options);
// Launch the Online Store Website
driver.get("file://" + YOUR_PATH_TO_HTML_FILE);
// You get all the paragraph as a list of WebElements
List<WebElement> paras = driver.findElements(By.tagName("p"));
// Then iterate
for (WebElement para : paras) {
// You can get text inside as this
System.out.println(para.getText());
}
<div id="response" class="clearFix">
<div class="clearFix paddingT10 paddingB10">
<span class="flL transaction_rejected transaction_icon">X</span>
<span class="flL transaction_htext">Transaction Rejected</span>
</div>
<div id="responseCustom">
<div style="margin-bottom: 10px;" class="portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all"><div class="portlet-header headercolor ui-state-default ui-corner-all dblclickbound mousedownbound"><span class="ui-icon ui-icon-minusthick flR"></span>Messages</div><div class="portlet-content contentcolor"><table width="50%"><tbody><tr></tr><tr><td><b>Host Reply Message:</b></td><td>ERR000 BOX NOT CLOSED</td></tr></tbody></table></div></div>
</div>
<div class="flL paddingT15 paddingB10 width99_percent">
<input type="button" class="btn_sty marginB10 ui-button ui-widget ui-state-default ui-corner-all" id="btnReSubmit" style="display:none" onclick="validateTranData(); return false" value="ReSubmit" role="button" aria-disabled="false">
</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.