How To Use Selenium In Python.

In this article, we will learn how to use Selenium web automation using python.

What is Selenium?

Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python.

Install Selenium using the following command:

pip install –U selenium

Prerequisites required for Selenium is Drivers:

=> Without a browser driver, selenium will not work. For that reason, you need to first install the driver with the chosen browser.

=> Selenium requires a driver to interface with the chosen browser. Chrome, for example, requires https://sites.google.com/chromium.org/driver/, which needs to be installed before the below examples can be run. Make sure it’s in your PATH, e. g., place it in /usr/bin or /usr/local/bin.

Other supported browsers will have their own drivers available. Links to some of the more popular browser drivers follow.

Chrome :   https://sites.google.com/chromium.org/driver/ 
Edge :        https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Firefox :    https://github.com/mozilla/geckodriver/releases
Safari :      https://webkit.org/blog/6900/webdriver-support-in-safari-10/

How to Install Chrome Driver for Chrome Browser:

Step 1 : First of all check your chrome browser version and then version-related driver file download using the above browser link.
=> Click on three dots which are shown on your browser’s right side.

Step 2 : The Next step is to click on Settings.

Step 3 : Go to About Chrome and show your browser version.
=> Once you are confirmed about a chrome version, let’s download the chrome driver. In my case, I’ll Download ChromeDriver 89.0.4389.23, and you may download the chrome driver according to the Chrome Browser version.

Step 4: Download your system-related zip file (64bit, 32bit).

Let’s start with one example,

=> First, of all, create one folder and create a selenium.py file
=> In, created folder under put your driver file above downloaded zip file unzip and then under you shown if like chrome browser then you got chromedriver.exe file. Put this chromedriver.exe file in your folder.

time : time module through you can wait for loading site and then perform next task
executable_path : PATH is your driver file path In our case chromedriver.exe file in our folder than path is “./chromedriver”.

import time
from selenium import webdriver

browser = webdriver.Chrome(executable_path="./chromedriver")
browser.get("https://www.google.co.in/")
print('Title: ', browser.title)
driver.find_element_by_name("q").send_keys("Automation testing")
button = driver.find_element_by_name("btnK")
time.sleep(2)
button.click()
time.sleep(5)
browser.quit()
  • .get Command to pass site URL which you can open and process
  • find_element_by_name through you can get particular name field and you can process on it.
  • send_keys using you can write any text in the browser

In, our example first of all finding the search field name is “q” and then using send_keys command through typing some text, and then finding button name “btnK” and some wait using time.sleep and click on its button.

You can also use the following commands for getting browser elements for automation,

find_element_by_id(“pass”)
find_element_by_class_name(“codehub”)
find_element_by_link_text(“login”)
find_element_by_xpath(“//input[@id=’’passwd]”)
find_element_by_partial_link_text(“login”)
find_element_by_tag_name(‘h1’)

Also, to find multiple elements, we can use

=> Above all commands get only one element if we find multiple elements then replace above command find_element to find_elements then you can get an array of multiple elements like.

elements = driver.find_elements_by_name(“password”)

=> get a text from element then use simply .text for getting all text,

textElement = find_element_by_id("codehub")
print(textElement.text)

I hope you’ll get impressed by the features of Selenium. So let’s start coding within the next blog. If you’ve got any questions regarding this blog, please let me know in the comments.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories