In this article, we are going to learn how to open chrome browser in selenium Python. To execute the test script we need to launch the chrome browser window. Today we will learn how we can do this by just using a simple script in selenium using the Python language.
By the end of this article, you will be able to answer the below questions.
- How to open/launch chrome browser in selenium python.
- How to invoke chrome browser in selenium python.
- How to open headless chrome browser in selenium python.
We can open the Chrome browser with the help of the web driver package from the Selenium framework. To do this, we need to install python and import the selenium and web driver package. To do the initial setup we have to follow these steps.
Once we import this package then using this, we can access the Chrome class to launch the browser window. Chrome browser can be launched by invoking chromedriver.exe as we have done in the below code example.
Once the browser is launched then it will be an open empty page window. So to set the URL in the address bar we can use the get() function with the URL that we want to access.
Open Chrome browser and type in address bar chrome://version/ to check for compatible version chrome driver as per Browser version. Now we can Download chromedriver from the official website.
1. How to open chrome browser in selenium Python
- After downloading chromedriver from the offical website .
- unzip and copy chromedriver.exe to the python script directory location.
- The path of the python script directory for Us is” C:\Users\Admin\AppData\Local \Programs \Python \Python310\Scripts”. It can be different depending on what location we have chosen while installing python.
- Run the below code to automatically open the chrome web browers.
from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.get("https://www.google.com") drive.quit()
- How to Install selenium on windows Python
- How to install selenium in VSCode
- How to open chrome browser in selenium Python
- How to open Edge browser in selenium Python
- How to open Firefox browser in selenium Python
- Executable path has been deprecated in selenium
- Solved version of ChromeDriver only supports Chrome version 98
- Solved selenium find_element_by_* commands are deprecated
- How to open new tab in Selenium Python
- How to open new window Selenium Python
2. Open a headless chrome browser using Selenium
Follow the above step to download and set up the chrome browser driver.exe. We will follow these steps To open a headless chrome browser.
- By using options class property specify those parameters,we need while launching the brower.In this example we have passed options.headless =True to open brower without UI.
- Driver.get(URL) :to open a specific webpage on chrome brower
- driver.title : To get name of brower on which we running our script.
- driver.Quit() to close the webbrower.
from selenium import webdriver from selenium.webdriver.chrome.options import Options optObj = Options() optObj.headless = True driver = webdriver.Chrome(options = optObj) driver.get('https://www.google.com') print(driver.title) driver.quit()
3. Open chrome browser Atuomatically in selenium Python
We manually download the web driver’s binary and unzip it at some location in the system and set up the path. It’s tedious!!! On top of that whenever the new version of the driver is released, you need to repeat all steps again and again. Instead of installing every webdriver separately and setting up the path manually. The webdriver-manager handles it itself
- The first step is Install the webdriver manager by using command “pip install webdriver-manager” and use it for different web browers .
- Write this code in the python code editor to launch the chrome browser.
from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager driver = webdriver.Chrome(ChromeDriverManager().install()) driver.maximize_window() driver.get("https://google.com") print('driver Title:',driver.title) print('Driver name:',driver.name) print('Driver URL:',driver.current_url) driver.quit()