Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to locate ALL elements by font-family #16808

Closed
1mzino opened this issue Aug 24, 2022 · 2 comments
Closed

[Question] How to locate ALL elements by font-family #16808

1mzino opened this issue Aug 24, 2022 · 2 comments

Comments

@1mzino
Copy link

1mzino commented Aug 24, 2022

Hi, I'm fairly new to Playwright.

I have a Playwright script that iterates over a list of URLs. The test is ran in parallel with other tests on different breakpoints and takes a full-page screenshot.

Essentially, the script is being used to identify where a certain font is being used and to highlight that element by changing background color:

    await page.evaluate(() => {
      document.querySelectorAll("*").forEach((el) => {
        if (
          getComputedStyle(el).fontFamily.includes("Telesans Agate") ||
          getComputedStyle(el).fontFamily.includes("Telesans Text") ||
          getComputedStyle(el).fontFamily.includes("Telesans-Text")
        ) {
          el.style.backgroundColor = "red";
        }
      });
    });

I want to learn how to locate and select all elements on the page, and then filter by 'font-family' so I can scrape other properties from these elements such as font-size, font-weight and save this data to a markdown file.

@yury-s
Copy link
Member

yury-s commented Aug 24, 2022

There is no builtin mechanism to filter by computed style properties but the approach you use should work:

    const styles = await page.evaluate(() => {
      return Array.from(document.querySelectorAll("*")).filter(el =>
          getComputedStyle(el).fontFamily.includes("Telesans Agate") ||
          getComputedStyle(el).fontFamily.includes("Telesans Text") ||
          getComputedStyle(el).fontFamily.includes("Telesans-Text")).map(e => {
             return {
                size: getComputedStyle(e)[font-size],
                weight:  getComputedStyle(e)[font-weight]
             };
          });
    });

Does this work for you?

@yury-s
Copy link
Member

yury-s commented Aug 29, 2022

Closing as part of the triage process since it seemed stale. Please create a new issue if you still face the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants