Selenium supports multiple browsers like Chrome, Firefox, IE, and Safari and so on. First of all, we have to import the webdriver package by adding from selenium import webdriver statement. (Give more context on this import)
After importing this base package, we will be exposed to all the browser classes within that package.
Now once we type webdriver. in our editor all the available classes shall be displayed and we can select the browser class like Chrome, Firefox or Ie within that list. The below image demonstrates the classes under webdriver.
Please Note: As a prerequisite to this step, we have to install Python, Selenium and set up PyCharm editor in our system. For step by step installation process, follow the below reference links:
To invoke Chrome browser
Now to invoke the chrome browser, we shall take the help of Chrome class within the webdriver package. We shall create a driver object which we will use in most of the automation steps.
Every browser like chrome, firefox and so on contain an executable file. With the help of Selenium methods we have to invoke the browser executable file and then the actual browser shall be invoked.
For getting the chrome executable file, we have to navigate to the Selenium official website: https://www.selenium.dev/downloads/. Then move to the Browsers section where all the Selenium supported browser sections will be available. For downloading the chrome browser driver, click on the documentation link under Chrome.
Based on the browser version we have in our system, we have to download that corresponding browser driver. The below image shows the various versions available in ChromeDriver for download.
Also based on the operating system (windows, Linux or mac) we are using, we have to download the corresponding browser driver. Once the download is completed, we shall get the file in .zip format. We have to extract that file and obtain the chromedriver.exe file then move that file to our project location.
We shall set the path of the executable file with the executable_path parameter within Chrome class and its access will be given to the driver object which shall now have the capabilities of the chrome browser.
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
Conclusion: Thus we have seen how to invoke the Chrome browser with Selenium in Python. For more details, you can refer to the link:
In the next section we shall discuss some of the basic Selenium webdriver methods.
Some basic webdriver methods
We can launch the URL in the browser with the help of the get method. The URL of the application is passed as an argument to the get method. After hitting the URL, we can obtain the URL of the application which has opened with the help of the current_url method and get its application title with the title method. The URL and title can be printed in the console log with the help of a print statement.
We can launch another URL with the get method and then we use the back method to move back to the old application. Similarly, we can move to the second application with the help of forward method. We can reload an existing page with refresh method.
Once an URL is launched in the browser, the application sometimes opens in a minimized window. To open the browser in the maximized mode maximize_window method is used. Again to minimize the window size, minimize_window method is used.
Finally we shall use the close method, to close the chrome browser that we have invoked.
Code to launch chrome browser along with some basic Selenium methods.
from selenium import webdriver
# setting the path of chromedriver.exe
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# get method to hit URL on browser
driver.get("https://rahulshettyacademy.com/")
# to maximize browser window
driver.maximize_window()
# get browser URL
print("Current URL: " + driver.current_url)
# get browser title
print("Current browser title: " + driver.title)
# get method to hit another URL on browser
driver.get("https://rahulshettyacademy.com/AutomationPractice/")
# to minimize browser window
driver.minimize_window()
# get browser URL after hitting the second URL
print("Second URL: " + driver.current_url)
# get browser title after hitting the second URL
print("Second browser title: " + driver.title)
# reload page
driver.refresh()
# move to previous page
driver.back()
# browser closing
driver.close()
Output
The output shows the URL and browser title after the application is launched in the chrome browser.
Conclusion
Thus we have discussed some of the basic webdriver methods in Selenium which are all invoked with the help of the driver instance. For more details, you can refer to the link:
In the next section we shall discuss how to invoke a firefox browser in Selenium webdriver.
To invoke Firefox browser
Now let us see how to invoke the firefox browser. We shall take the help of Firefox class within the webdriver package and also use the driver object we have already created.
For getting the firefox executable file, we have to navigate to the Selenium official website: https://www.selenium.dev/downloads/. Then move to the Browsers section where all the Selenium supported browser sections will be available. For downloading the firefox browser driver, click on the documentation link under Firefox.
GeckoDriver is the driver name used for firefox browsers. Next we need to download the geckodriver version which is compatible with the Selenium version we are using. The below image shows the list geckodriver versions available which are compatible with the corresponding Selenium versions.
Next click on geckodriver releases link on the page. Also based on the operating system (windows, Linux or mac) we are using, we have to download the corresponding browser driver. Once the download is completed, we shall get the file in .zip format. We have to extract that file and obtain the geckodriver.exe file then move that file to our project location. The below image shows the list geckodriver files available for different types and configurations of operating systems.
We shall set the path of the executable file with the executable_path parameter within Firefox class and its access will be given to the driver object which shall now have the capabilities of the firefox browser.
driver = webdriver.Firefox(executable_path="C:\\geckodriver.exe")
Code to invoke firefox browser.
from selenium import webdriver
# setting the path of geckodriver.exe
driver = webdriver.Firefox(executable_path="C:\\geckodriver.exe")
# get method to hit URL on browser
driver.get("https://rahulshettyacademy.com/")
# get browser URL
print("Current URL: " + driver.current_url)
# get browser title
print("Current browser title: " + driver.title)
Output:
The output shows the URL and browser title after the application is launched in the firefox browser. In Python, if execution ends with a Process finished with exit code 0 statement it means that test execution completed without errors. The output contains the current URL - https://www.rahulshettyacademy.com/#/index and the browser title Rahul Shetty Academy.
Thus we have seen how to invoke the Firefox browser with Selenium in Python. For more details, you can refer to the link:
In the next section we shall discuss how to invoke an internet explorer browser in Selenium webdriver
To invoke Internet Explorer browser
Now let us see how to invoke the internet explorer browser. We shall take the help of Ie class within the webdriver package and also use the driver object we have already created.
For getting the internet explorer executable file, we have to navigate to the Selenium official website: https://www.selenium.dev/downloads/. Then move to The Internet Explorer Driver Section and proceed depending upon the configuration of the operating system we are using.
Once the executable file is downloaded, we shall get the file in .zip format. We have to extract that file and obtain the IEDriverServer.exe file then move that file to our project location.
We shall set the path of the executable file with the executable_path parameter within Ie class and its access will be given to the driver object which shall now have the capabilities of the internet explorer browser.
driver = webdriver.Ie(executable_path="C:\\IEDriverServer.exe")
Code to invoke internet explorer browser.
from selenium import webdriver
# setting the path of IEDriverServer.exe
driver = webdriver.Ie(executable_path="C:\\IEDriverServer.exe")
driver.get("https://rahulshettyacademy.com/")
Conclusion
Thus we have seen how to invoke the Internet Explorer browser with Selenium in Python. For more details, you can refer to the link:
In the next post, we shall discuss different locators in Selenium webdriver API.