Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get text of td element using selenium?

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?

like image 505
Prasanta Biswas Avatar asked Mar 10 '23 03:03

Prasanta Biswas


2 Answers

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

driver.findElement(By.xpath("//table[@id='myTable']/tbody/tr/td")).getAttribute("innerHTML");
like image 123
Guy Avatar answered Mar 21 '23 00:03

Guy


Use the following xpath (java code) -

String result = driver.findElement(By.xpath(".//*[@id='myTable']//td[contains(.,'You have transferred')]")).getText();
like image 21
NarendraR Avatar answered Mar 20 '23 22:03

NarendraR