Selenium is a portable framework for testing web applications. Selenium automates the web browser so that you can test your web applications on the web browser in an automated mode.

Python Selenium:

Here we are going to see a very first Selenium HelloWorld example using Python.

Prerequisites:

Versions:

I am running this example on the below software versions.

  • Python3.7
  • pip 20.1
  • Selenium 3.141
  • Chrome Browser  81.0.4044.138
  • Chrome Driver 83.0.4103.39
The Chrome browser and driver should be incompatible mode. If driver version not compatible with your browser version you may have to upgrade or downgrade your browser.

Installations:

Install Python – I am suspecting you have installed Python on your machine if you haven’t done so far you can follow my previous tutorials to install python on windows 10.

Install Pip – The pip is a package manager for python, it is used to install the new python packages, this has been installed as part of the python installation. You no need to install it manually.

Install selenium – To install selenium you can use the pip package manager. Open a command prompt and navigate to the pip installation directory. and it the below command to install selenium.

Note: If you have old pip in your system, you should have to upgrade the pip to install the new packages, otherwise you may get installation errors.

pip install selenium

The above command installs the selenium, so now you can make you of it.

Install Chrome Browser – I am not discussing how to install the chrome browser in this tutorials, I guess you aware of it.

Install Chrome Driver – As we earlier discussed Selenium is used to automate the web browser so that to open the web browser programmatically, we need to have web driver running in our system, Download the ChromeDriver it comes with a zip file, extract it where you can see chromedriver.exe file.

Installations are done, now we can start code for Python selenium HelloWorld example.

Python selenium HelloWorld example:

As part of this example, I am going to access the google.com page in a selenium web browser.

selenium_helloworld.py
from selenium import webdriver

driver = webdriver.Chrome(executable_path="D:\softwares\chromedriver_win\chromedriver.exe")
driver.get("http://www.google.com")
print(driver.title)
driver.close()

Code Walkthrough:

  • importing webdriver form selenium package
  • webdriver.Chrome() function is used to launch the web browser. for this, we have to provide the chromedriver.exe path as an executable_path=”{chromedriverpath}”
  • diver.get("URL") function loads a web page in the current browser session.
  • driver.title returns the title of the current web page.
  • Finally closing the driver using the close() function.

Output:

C:\Users\Lenovo\python_samples>python selenium_helloworld.py
Google

You could see the web browser opened after running the above program like below.

Python Selenium HelloWorld Example

Happy Learning 🙂