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()
Posts You Might Like
- Why playwright is better than selenium webdriver, is it?
- Handle dropdowns in Playwright Python
- Open the browser and Close in Playwright Python
- Handle checkbox in Playwright Python
- Element Operations in Playwright Python
- Handle IFrames in Playwright Python
- Page level commands in Playwright Python
- Element State with Playwright Python