In this Python Selenium lesson we want to learn How to Install Selenium Web Driver in Python, we are going to learn how to install Python Selenium, how you can add Chrome and Firefox drivers in Python Selenium and at the end we want to create a simple example in Selenium Web Driver using Python Programming Language.
What is Selenium ?
Selenium is a set of tools for browser automation, and it is mostly used for testing applications. but the usage of selenium is not limited to testing of applications, but it can also be used for screen scrapping and repetitive tasks automation in a browser. so selenium supports automation for different browsers like firefox, internet explorer, google chrome, safari and opera.
There are different tools of selenium that you can use
- Selenium IDE: This is a Firefox add-in used to record and play back the
Selenium scripts with Firefox. It provides a graphical user interface to
record user actions using Firefox. - Selenium Web Driver: This is programming interface, and it is used with programming languages like Java,Python,C#, Ruby, PHP and JavaScript. and the web drivers supports different browsers like, Google Chrom, Mozila Firefox, Internet Explorer, Opera and Safari.
- Selenium Stand Alone Server: This is also known as Selenium Grid and allows remote and distrbuted execution of Selenium scripts created with WebDriver.
What is Python Selenium ?
Python Selenium bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium API you can access all functionalities of Selenium WebDriver in an intuitive way. Selenium bindings provide a convenient API to access Selenium WebDrivers like Firefox, Ie, Chrome, Remote etc. The current supported Python versions are 2.7, 3.5 and above.
Installation
For the installation you can just use pip.
1 |
pip install selenium |
Drivers
Selenium requires a driver to interface with the chosen browser, and there are different driver for each browsers that you need to download, it is just an exe file, after downloading just copy and paste the driver where you have installed your python. these are the most popular drivers download link for different browsers.
After installation now you need to import selenium web driver.
1 |
from selenium import webdriver |
If you want to open a browser than you can use this code, in here i have used Chrome and FireFox browsers.
1 2 |
driver = webdriver.Chrome() #driver = webdriver.Firefox() |
If you want to open a website than you can add this code.
1 |
driver.get('https://www.geekscoders.com') |
This is the code.
1 2 3 4 5 6 |
from selenium import webdriver driver = webdriver.Chrome() #driver = webdriver.Firefox() driver.get('https://www.geekscoders.com') |
Run the code and this is the result.
These are some web properties for the driver, like printing the title of the web page, current url and page source.
1 2 3 |
print(driver.title) print(driver.current_url) print(driver.page_source) |
And these are some web browser commands, for example you can set the screen of the browser and also x and y position of the browser.
1 2 3 4 5 |
driver.maximize_window() #driver.fullscreen_window() #driver.set_window_size(400,400) #driver.set_window_rect(x=200, y=200, width=200,height=200) driver.close() |