java 如何使用硒获取 td 元素的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41712882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get text of td element using selenium?
提问by Prasanta Biswas
I have an html table. I need to get the text of a td element with selenium.
我有一个 html 表。我需要用硒获取 td 元素的文本。
Html structure:
html结构:
<table id="myTable">
<tbody>
<tr>
<td>
<b>Success: </b>
You have transferred 1,000.00 USD to DIST2. Your balance is now 19,979,000.00 USD. ref: 2017011806292760301000301
</td>
</tr>
</tbody>
</table>
I tried using driver.findElement(By.xpath("//table[@id='myTable']/tbody/tr/td")).getText();
我尝试使用 driver.findElement(By.xpath("//table[@id='myTable']/tbody/tr/td")).getText();
It is returning blank string. I need to get "You have transferred 1,000.00 USD to DIST2. Your balance is now 19,979,000.00 USD. ref: 2017011806292760301000301" from it. I think the td element contains a tag that is why it is not returning the value.
它正在返回空白字符串。我需要从中获得“您已将 1,000.00 美元转至 DIST2。您的余额现在为 19,979,000.00 美元。参考:2017011806292760301000301”。我认为 td 元素包含一个标签,这就是它不返回值的原因。
Is there any way to fetch the value?
有什么方法可以获取值吗?
回答by Guy
Your locator is correct. Try getAttribute("innerHTML")
instead of getText()
您的定位器是正确的。尝试getAttribute("innerHTML")
代替getText()
driver.findElement(By.xpath("//table[@id='myTable']/tbody/tr/td")).getAttribute("innerHTML");
回答by NarendraR
Use the following xpath
(java code) -
使用以下xpath
(java代码) -
String result = driver.findElement(By.xpath(".//*[@id='myTable']//td[contains(.,'You have transferred')]")).getText();