繁体   English   中英

Selenium JS如何获取元素的文本

[英]Selenium JS how to get text of an element

我已经搜索了一个答案,这应该相对容易,但是我如何从浏览器中的填充表格中获取文本?

“检查”看起来像这样:

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

显然桌子上还有比这更多的东西,但我只想能够抓住那段文字?

我会做类似的事情吗

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

因为那行不通。 或者由于表中有多个元素,我应该

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

这两种方法我都试过了。

要提取文本The Text I want您可以使用以下任一Locator Strategies

  • 使用类名

     await driver.findElement(By.className("component-body-text").getText()
  • 使用css

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

     await driver.findElement(By.xpath("//tr[@role='row']/th[@class='component-body-text' and @role='columnheader']").getText()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM