How to get text of td element using selenium?

左心房为你撑大大i 提交于 2020-02-04 05:29:05

问题


I have an html table. I need to get the text of a td element with selenium.

Html structure:

<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();

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.

Is there any way to fetch the value?


回答1:


Use the following xpath (java code) -

String result = driver.findElement(By.xpath(".//*[@id='myTable']//td[contains(.,'You have transferred')]")).getText();



回答2:


Your locator is correct. Try getAttribute("innerHTML") instead of getText()

driver.findElement(By.xpath("//table[@id='myTable']/tbody/tr/td")).getAttribute("innerHTML");


来源:https://stackoverflow.com/questions/41712882/how-to-get-text-of-td-element-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!