1.WebElement webElemt= webElement.findElement(By.xpath("//*[@class='xxxxxx]/a"));
//在ie下使用innerText 在火狐下使用textContent
String linktext = webElemt.getAttribute("innerText"); 2.WebElement element = ...;//测试的好像不行。 ((JavascriptExecutor) driver).executeScript("return arguments[0].getText();", element);
Example:
<div class="userinfobox2 clearfix">
<p class="userrankbox1 pull-left"><strong>233</strong>
<span>
<a href="javascript:void(0)">发送数</a>
</span>
</p>
<p class="userrankbox1 pull-left"><strong>97</strong><span>
<a href="javascript:void(0)">收到数</a>
</span></p>
</div>
1.1 WebElement findElement = driver.findElement(By.xpath("//div[@class='userinfobox2 clearfix']//strong[1]"));
System.out.println("findelement:" + findElement.getText()); //233
1.2 WebElement findElement2 = driver.findElement(By.xpath("//div[@class='userinfobox2 clearfix']//strong[last()]"));
System.out.println("findElement2:" + findElement2.getAttribute("innerText")); //233
1.3 WebElement findElement4 = river.findElement(By.xpath("//p[@class='userrankbox1 pull-left'][1]/strong"));
System.out.println("findElement4:"+findElement4.getText()); //233
1.4 WebElement findElement5 = driver.findElement(By.xpath("//p[@class='userrankbox1 pull-left'][2]/strong"));
System.err.println("findElement5:"+findElement5.getText());//97