如何使用Java从Selenium Webdriver获取Span的文本

时间:2015-02-05 12:39:37

标签: java html css xpath selenium-webdriver

我正在尝试从span元素中获取文本值:

<td class="cs data">
<span id="ctl00_wnCore_ctlRiskDetails_ctdRiskDetails_rptCommon2Column_ctl01_lblValue">US00002277EQ15A</span>
</td>

我需要获取文本值:US00002277EQ15A(或者这个值恰好是什么)但是以下示例都不起作用,它们总是返回“N / A”

示例1

String policyNumber =   driver.findElement(By.xpath("//span[@id='ctl00_wnCore_ctlRiskDetails_ctdRiskDetails_rptCommon2Column_ctl01_lblValue']")).getAttribute("innerHTML");

System.out.println(policyNumber);

示例2

String policyNumber =   driver.findElement(By.xpath("//span[@id='ctl00_wnCore_ctlRiskDetails_ctdRiskDetails_rptCommon2Column_ctl01_lblValue']")).getText();

示例3

String policyNumber =   driver.findElement(By.cssSelector("html body form#aspnetForm div#canvas div#main div#rhs div#main_content div.client.aspx.transactionsummary div.core.ascx.products.riskdetails div.core.ascx.products.commontabledisplay div#ctl00_wnCore_ctlRiskDetails_ctdRiskDetails_divTwoColumnDisplay table.stdtable tbody tr#trAlternatingItemTemplate td.c2.data span#ctl00_wnCore_ctlRiskDetails_ctdRiskDetails_rptCommon2Column_ctl01_lblValue")).getText();

任何帮助将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:0)

尝试使用javascript

获取文字
JavascriptExecutor executor = (JavascriptExecutor) driver;
String text=(String)executor.executeScript("return document.getElementById("enter span id"));

告诉我它是否有效