Selenium JS how to get text of an element

I have searched for an answer, and this should be relatively easy but how do I go about grabbing the text from a populated table in a browser?

The "inspect" looks something like this:

<tr role="row">
  <th class="component-body-text" colspan="1"role="columnheader">### The Text I want ###</th>

Obviously there is more to the table than this, but I just want to be able to grab that piece of text?

would I do something like

await driver.findElement(By.className('component-body-text')).getText(); 

because that does not work. Or since there are multiple elements in the table should I

const sample = await driver.findElements(By.className('component-body-text')); 
sampleText = sample[0].getText();

I have tried both methods.

93 thoughts on “Selenium JS how to get text of an element”

  1. 944080 804264Spot lets start function on this write-up, I really believe this wonderful internet site requirements significantly much more consideration. Ill apt to be once once more to read an excellent deal a lot more, a lot of thanks for that info. 470266

  2. Love doll online store | Brand genuine product | Cheapest direct sales store:
    Adult LOVE (otona love) is a regular direct online sales store and has its own factory. Since we deliver to customers directly from the factory without going through a third party, we can offer love dolls at a low price and high quality.