     [Answers](https://scrapfly.io/blog)   /  [playwright](https://scrapfly.io/blog/tag/playwright)   /  [How to check if element exists in Playwright?](https://scrapfly.io/blog/answers/how-to-check-for-element-in-playwright)   # How to check if element exists in Playwright?

 by [Bernardas Alisauskas](https://scrapfly.io/blog/author/bernardas) Apr 18, 2023 1 min read [\#playwright](https://scrapfly.io/blog/tag/playwright) [\#python](https://scrapfly.io/blog/tag/python) 

 [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-check-for-element-in-playwright "Share on LinkedIn")    

 

 

To check whether an HTML element is present on the page in Playwright and Python we can use the `page.locator()` or `page.is_visible()` functions:

python```python
with sync_playwright() as pw:
    browser = pw.chromium.launch(headless=False)
    context = browser.new_context(viewport={"width": 1920, "height": 1080})
    page = context.new_page()

    # go to url
    page.goto("https://scrapfly.io/")

    # use .locator() with CSS or XPath selectors:
    elements = page.locator("div.post-content")
    if elements.count() > 0:  
        print(f"found {elements.count()} elements")
        visible = sum([handle.is_visible() for handle in elements.element_handles()])
        print(f"out of which {visible} are visible")
```



Above, we use the `.locator` method to allow Playwright check if element exists. Based on the DOM element locating state, we decide how to proceed with the web scraping process.

The above code snippet is inteted to use with the Python playwright client. Here's how to check if an element exists on the page in the JavaScript Playwright client:

javascript```javascript
const { chromium } = require('playwright');

(async () => {
    const browser = await chromium.launch({ headless: false });
    const context = await browser.newContext({
        viewport: { width: 1920, height: 1080 }
    });
    const page = await context.newPage();

    // go to url
    await page.goto('https://scrapfly.io/');

    // use .locator() with CSS selectors:
    const elements = await page.locator('div.post-content');
    const count = await elements.count();
    if (count > 0) {
        console.log(`found ${count} elements`);
        let visible = 0;
        for (let i = 0; i < count; i++) {
            const handle = await elements.nth(i);
            if (await handle.isVisible()) {
                visible++;
            }
        }
        console.log(`out of which ${visible} are visible`);
    }

    await browser.close();
})();
```



Note that this method will **not wait** for the element to appear on the page. For waiting see [How to wait for page to load in Playwright?](https://scrapfly.io/blog/answers/how-to-wait-for-page-to-load-in-playwright)



 

    



Scale Your Web Scraping

Anti-bot bypass, browser rendering, and rotating proxies, all in one API. Start with 1,000 free credits.

  No credit card required  1,000 free API credits  Anti-bot bypass included 

 [Start Free](https://scrapfly.io/register) [View Docs](https://scrapfly.io/docs/onboarding) 

 Not ready? Get our newsletter instead. 

 

## Explore this Article with AI

 [ ChatGPT ](https://chat.openai.com/?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-check-for-element-in-playwright) [ Gemini ](https://www.google.com/search?udm=50&aep=11&q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-check-for-element-in-playwright) [ Grok ](https://x.com/i/grok?text=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-check-for-element-in-playwright) [ Perplexity ](https://www.perplexity.ai/search/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-check-for-element-in-playwright) [ Claude ](https://claude.ai/new?q=Summarize%20this%20page%3A%20https%3A%2F%2Fscrapfly.io%2Fblog%2Fanswers%2Fhow-to-check-for-element-in-playwright) 



 ## Related Articles

 [     

 python playwright 

### What is Screen Scraping? Developer's Guide with Python Examples

Capture data from visual output when no API exists using browser automation to extract data from JavaScript-rendered pag...

 

 ](https://scrapfly.io/blog/posts/what-is-screen-scraping) [  

 blocking 

### How to Bypass PerimeterX when Web Scraping in 2026

In this article we'll take a look at a popular anti scraping service PerimeterX. How does it detect web scrapers and bot...

 

 ](https://scrapfly.io/blog/posts/how-to-bypass-perimeterx-human-anti-scraping) [  

 blocking 

### How to Bypass Akamai when Web Scraping in 2026

In this article we'll take a look at a popular anti bot service Akamai Bot Manager. How does it detect web scrapers and ...

 

 ](https://scrapfly.io/blog/posts/how-to-bypass-akamai-anti-scraping) 

  ## Related Questions

- [ Q How to find elements by CSS selectors in Playwright? ](https://scrapfly.io/blog/answers/how-to-find-elements-by-css-selectors-in-playwright)
- [ Q How to find elements by XPath selectors in Playwright? ](https://scrapfly.io/blog/answers/how-to-find-elements-by-xpath-in-playwright)
- [ Q How to take a screenshot with Playwright? ](https://scrapfly.io/blog/answers/how-to-take-screenshot-with-playwright)
- [ Q How to capture background requests and responses in Playwright? ](https://scrapfly.io/blog/answers/how-to-capture-xhr-requests-playwright)
 
  



   



 Run headless browsers at scale, **1,000 free credits** [Start Free](https://scrapfly.io/register)