Search code examples
pythonplaywrightplaywright-python

How do I read the content of a span class with python playwright


I want to read the score from a webpage. The score is located in:

<span _ngcontent-fnp-c148="" class="score"> &nbsp;<!--bindings={
"ng-reflect-ng-if": "false"
}--> 96%, </span>

Here the result would be "96%"

I'd like to either search for the classname "score" directly, or if it's necessary dump the entire page html into a string and search for it manually. An approach to do either would be fine.

I'm using python playwright, but I assume the methods should be similar to the original.


Solution

  • You can use the Page.innerText(".score") method to accomplish that, see here for an example:

    // @ts-check
    const playwright = require("playwright");
    
    (async () => {
      const browser = await playwright.chromium.launch();
      const context = await browser.newContext();
      const page = await context.newPage();
      await page.setContent(`
        <span _ngcontent-fnp-c148="" class="score"> &nbsp;<!--bindings={
    "ng-reflect-ng-if": "false"
    }--> 96%, </span>
    `);
      const content = await page.innerText(".score")
      console.log(content)
      await page.screenshot({ path: `example.png` });
      await browser.close();
    })();
    

    Or interactively here: https://try.playwright.tech/?s=bl1fed4

    For Python usage see here: https://microsoft.github.io/playwright-python/sync_api.html#playwright.sync_api.Page.innerText