簡體   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