Updated April 3, 2023
Introduction to Selenium Webdriver
Webdriver is a web automation framework that enables us to use a programming language in order to execute scripts in different web browsers like Google Chrome, Mozilla Firefox, Microsoft Edge, etc. It controls the browser from the OS level. All it requires is a script to run and a web browser. Selenium web driver supports different programming languages like Java, Python, Ruby, Perl, PHP, .NET, etc.
Requirements:
- Selenium package: This can be installed by using the following command.
pip install selenium
- Drivers: Different browsers will have different drivers that are needed to be downloaded before using them.
- For chrome: chromium driver, download it from the following link.
https://sites.google.com/a/chromium.org/chromedriver/downloads
- For Firefox: Gecko driver, download it from the following link.
https://github.com/mozilla/geckodriver/releases
- For Microsoft Edge: internet explorer driver, download it from:
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
- For safari: Safari driver, download it from:
https://webkit.org/blog/6900/webdriver-support-in-safari-10/
- For windows users, after downloading the required driver, unzip the file.
- Place the file in a directory and save the path to that directory.
- Add the path in the environment variables as a path, and save the path for later use.
Usage of Selenium WebDriver
- From the selenium package we need to import web driver, this can be done by:
from selenium import webdriver
- Create a driver by using the following code:
driver = webdriver.Firefox('path to geckodriver')
Here, for instance, I have used geckodriver for the firefox browser you can use anything instead.
- Pick a url where this webdriver has to head and place the url inside driver.get() method, this method automates the driver towards the url. For instance, let us use url to search selenium in Wikipedia.
driver.get('https://en.wikipedia.org/wiki/Selenium')
- To automatically close the browser after opening it use driver.quit() at the end of the code.
- Summing up all the above steps the following will be the python script,
from selenium import webdriver
driver = webdriver.Firefox('path to geckodriver')
driver.get('https://en.wikipedia.org/wiki/Selenium')
driver.quit()
- The above web page gets opened after running the above script, but the page automatically gets closed because of driver.quit() option so we can use that as optional.
- This selenium web driver also supports the HtmlUnit browser which is used for headless browsing which means the webpage can be accessed without opening the web browser, this can be done as follows.
- Import options from selenium as:
from selenium.webdriver.firefox.options import Options
- Set the options to headless and then include it along with the path of the driver as:
options = Options()
driver = webdriver.Firefox('path to geckodriver',options = options)
- Web driver provides many number of ways to find the elements from the web page through xpath, is, name, css_name, css_selector etc.
- Locating elements can be used as follows,
driver.find_element_by_xpath('xpath value')
all the values for the locating elements can be found by inspecting the web page.
Let us consider another example by using locating elements,
- Here we will work login automation for a website called Quora.
- First, let us get the URL path for Quora website and include it in the driver.get()
- Then the page might look like this,
- Now inspect each element by right-clicking on the element and select inspect element option to find the value of the element as shown below.
- Copy the value of the locating element and according to the value selected use the following code,
Email = driver.find_element_by_id(id = 'value of id')
- In the above-given website, the values of email id and password has to be entered which can be done by,
Email.send_keys('enter the email id')
- Similarly, enter all the required fields and then we have to press the login button to do this inspect the value of login button, and then write the following code.
Button = driver.find_element_by_id(id = 'value of login button')
Button.click()
- Then automatically the website will be redirected to the account as shown below.
- Summing up all the above steps python script will be as below.
from selenium import webdriver
driver = webdriver.Firefox('C:\\geckodriver')
driver.get('https://www.quora.com/')
email = driver.find_element_by_name("email")
email.send_keys('enter the email id')
password = driver.find_element_by_name('password')
passsword.send_keys('enter the password')
button = driver.find_element_by_xpath('//*[@id="__w2_wQpenGLD21_submit_button"]')
button.click()
Locating Elements with Selenium:
There are different ways to locate elements in a web page. The following are the different methods to locate elements.
Example #1: Find element by id
We can use this method when we know id attribute of an element. Let say we have a page source as follows.
Code:
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
</form>
</body>
<html>
We can locate the form below
login_form = driver.find_element_by_id('loginForm')
Example #2: Find element by name
We can use this when we know the name attribute of an element. Consider the following source.
Code:
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
<input name="continue" type="button" value="Clear" />
</form>
</body>
<html>
We can extract username and password like this.
username = driver.find_element_by_name('username')
password = driver.find_element_by_name('password')
Example #3: Find element by XPath
With XPath consider the page source as follows.
Code:
<html>
<body>
<form id="loginForm">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="continue" type="submit" value="Login" />
<input name="continue" type="button" value="Clear" />
</form>
</body>
<html>
Elements can be located like this.
login_form = driver.find_element_by_xpath("/html/body/form[1]")
login_form = driver.find_element_by_xpath("//form[1]")
login_form = driver.find_element_by_xpath("//form[@id='loginForm']")
Example #4: Find element by line_text
We can locate hyperlinks with link text as follows. Consider the page source as follows.
Code:
<html>
<body>
<p>Are you sure you want to do this?</p>
<a href="continue.html">Continue</a>
<a href="cancel.html">Cancel</a>
</body>
<html>
Continue link can be located like this.
continue_link = driver.find_element_by_link_text('Continue')
continue_link = driver.find_element_by_partial_link_text('Conti')
Example #5: Locate Elements by CSS Selector
This method allows you to locate elements by class attribute name. Below is the source.
Code:
<html>
<body>
<div class="round-button">Click Here</p>
</body>
<html>
We can locate css sector as below.
get_div = driver.find_element_by_css_selector('div.round-button')
Example #6: Locate Elements by Tagname
This method allows you to find a web-element by specifying the tag name.
Code:
<html>
<body>
<title>Hello Python</title>
<p>Learn test automation using Python</p>
</body>
<html>
The above code has a title tag with some text. You can find it using the below code.
get_div = driver.find_element_by_tag_name('title')
You can find the reference link below.
https://selenium-python.readthedocs.io/installation.html#introduction
Recommended Articles
We hope that this EDUCBA information on “How to Use Selenium?” was beneficial to you. You can view EDUCBA’s recommended articles for more information.