简体   繁体   English

如何在<span>使用 puppeteer 中获取文本?</span>

[英]How can I get text inside of <span> using puppeteer?

Having the following html:具有以下 html:

<div class="col-xs-6 col-md-6 nopaddingright">
    <ul class="account-statics clearright">
        <li>
          <span class="title">Title1</span>
          <span class="value fs13 ">value1</span>
        </li>
        <li>
          <span class="title">Title2</span>
          <span class="value fs13 ">value2</span>
          <ul class="account-sub">
            <li>
              <span class="title">Title3</span>
              <span class="value fs13 ">value3</span>
            </li>
            <li>
              <span class="title">Title4</span>
              <span class="value fs13 ">value4</span>
            </li>
          </ul>
        </li>
        <li>
          <span class="title">Title5</span>
          <span class="value fs13 ">value5</span>
        </li>
    </ul>
</div>

I wanna get the text inside of all second elements.我想在所有第二个元素中获取文本。

I tried this way:我试过这样:

const element = await page.$('div > ul[class="account-statics clearright"] > ul > li:nth-child(2) > span:nth-child(2)');

And another way:另一种方式:

const elements= await page.$$('span[class="value fs13 "]');

But both attempts didn't work.但两次尝试都没有奏效。 How can I solve this?我该如何解决这个问题?

const sp = await page.$x('//*[contains(@class, "fs13")]');    
const spanName = await (await sp.getProperty('textContent')).jsonValue();

The following code will get you the span text assuming the class name in span is fs13.假设 span 中的类名称为 fs13,以下代码将为您提供 span 文本。

There shouldnot be '.'不应该有'.' infront of fs13 in $x function. $x 函数中 fs13 的前面。 It should be only fs13 as we have already mentioned it is class它应该只是 fs13,因为我们已经提到它是类

sp.getProperty is not a function sp.getProperty 不是 function

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

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