...
×

Open the browser and Close in Playwright Python

When we talk normally page and browser look or mean kind of the same but there is a difference. Think page as a tab on a browser, you can open multiple tabs on a browser. But the moment you close all the tabs one after the other finally the browser closes when we close the final tab. In reality, the browser does not close when we close the last remaining tab, it is not just visible.

You can open a browser using launch function from the playwright, you can also make choices whether you want headed or headless browser and other features.

from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)

Then you can open a tab, once you open a tab, only then you will start to see the browser, till this time browser is in the process level only.

from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()

With Playwright, you can work with different browsers like Chrome, Chromium, Firefox, and Safari WebKit.

Chromium browser (chrome and edge)

For this, all you need to do is change which browser you want to open just like the below code.

from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()

browser = playwright.chromium.launch(headless=False) # this line

page = browser.new_page()
page.goto("https://google.com")
page.locator("[name='q']").fill("testercoder")

In the above code, I have opened the Chromium browser.

Firefox browser

Now let’s open the Firefox browser in the below code.

browser = playwright.firefox.launch(headless=False)

Safari Browser:

In Playwright, the Safari browser is also known as the Webkit browser.

browser = playwright.webkit.launch(headless=False)

Chrome Browser

Let’s Open the Chrome browser that is installed on the local machine. For this, we need to know the path of Chrome installation in the local system. And pass the path to the “executable_path” parameter in launch() function. Similarly, you can also run Firefox browser.

from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False, executable_path="/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
page = browser.new_page()
page.goto("https://google.com")
page.locator("[name='q']").fill("testercoder")

Note: In Windows OS right-click on Chrome to find the .exe path of Chrome, but on MacOS go to the application folder and open the Chrome application and so on.

Close the page:

You can close the page using page.close(), this will close the current opened webpage.

from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://google.com")
page.close()

Close the browser:

If you have multiple tabs opened and if you want to close the complete browser then use the close() function on the browser object.

from playwright.sync_api import sync_playwright

playwright = sync_playwright().start()
browser = playwright.chromium.launch(headless=False)
page = browser.new_page()
page.goto("https://google.com")
browser.close()

Author :

Pavankumar Nagaraj is an automation testing expert with over 12 years of experience, specializing in tools like Selenium, Protractor, Puppeteer, and Playwright, and skilled in Java, Python, and JavaScript

Pavankumar Nagaraj
Pavankumar Nagaraj

Pavankumar Nagaraj is an automation testing expert with over 12 years of experience, specializing in tools like Selenium, Protractor, Puppeteer, and Playwright, and skilled in Java, Python, and JavaScript

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.