0

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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jason V
  • 623
  • 1
  • 10
  • 30

1 Answers1

0

To extract the text The Text I want you can use either of the following Locator Strategies:

  • Using className:

    await driver.findElement(By.className("component-body-text")).getText();
    
  • Using css:

    await driver.findElement(By.css("tr[role='row'] > th.component-body-text[role='columnheader']")).getText();
    
  • Using xpath:

    await driver.findElement(By.xpath("//tr[@role='row']/th[@class='component-body-text' and @role='columnheader']")).getText();
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    className did not work for me. error message was "a device attached to the system is not functioning" css worked as is. I mean, I fixed the parenthesis but other than that, excellent. thank you. – Jason V Feb 17 '21 at 13:48
  • I see that someone downvoted. May I ask why? What I said in my comment was true. The suggestion worked out of the box for me. (again, className did not.) – Jason V Feb 18 '21 at 14:15