Selenium python tutorial for beginner : Get web scraping using selenium python tutorial. The python tutorial will provides a simple API acceptance test using the selenium web driver. Using the selenium API access you can use all the functionalities of selenium driver. The python will provide API access to selenium web drivers like remote, chrome, etc. The open-source tool is used for automating test cases on web browsers. The selenium tool is a web automation tool used to control the browser by python code. All the basic and advance concept of selenium is provided here and designed for beginners, professionals. It is used for open source Web UI (user interface) for testing suite. Selenium is developed by Jason Huggins in 2004 as the internal tool.
The selenium tool will support the automation across different browsers and programming language.
The platforms where selenium is used are Solaris, Linux, Windows and Macintosh.
Selenium also supports the operating system for applications like android, windows mobile, etc.
It will support programming language to use drivers of language supported by selenium.
Initially the selenium web driver is used with c# and JAVA.
The tool is used for the web applications to support cross operating system and cross browser.
The testing which is done by using the selenium tool is called selenium testing.
The selenium testing is used for testing the web applications but not used for desktop and mobile applications.
Basically selenium tool is the framework used to automate the web browsers and support python programming language.
Use of this tool is work on web scrapping and then JavaScript become easy.
Selenium tool has its specific language called as “selenese” that represent selenium command used for writing the test cases.
Selenium tool is test web application used in instance,
pip install selenium / pip install –U selenium
The downloading is done by binding the selenium from PypI page of selenium.
Pip is used to install packages.
Selenium will support python and utilize selenium with testing:-
Sr.No |
Command and Description |
Element.sendKeys(“inputtext”) |
It will enter some text into an input box. |
Driver.get(“URL”) |
Used to navigate an application. |
Element.clear() |
It clears the content from input box. |
Select.deselectAll() |
It deselects OPTION from FIRST select on page. |
Driver.selectByVisibleText(“some text”) |
It selects OPTION with the input which is specified by user. |
Driver.switchTo().window(“windowName”) |
It move and focus from one window to another. |
Driver.switchTo().alert() |
It will help in handling the alerts. |
Driver.navigate().to(“URL”) |
It will navigate to URL |
Driver.navigate().forward() |
It will navigate forward. |
Driver.navigate().back() |
It will navigate back. |
Driver. close() |
It will close the current browser associated with driver. |
Driver. quit() |
It quits the driver and closes all windows of driver. |
Driver. refresh() |
It quits will refresh the current page. |
The selenium webdriver is the automation tool used to automate the test scripts written in selenium.
Webdriver installation:-
Chrome:https:\\sites.google.com/a/chromium.org/chromedriver/downloads
Libraries imported are:-
From selenium import webdriver
Import time
Selenium library:-
It is used for the automation and control web driver also performs actions like goto website link, etc.
Time library:-
Used for sleep function because selenium works when all elements of pages are loaded.
From selenium import webdriver
Import time
Browser=webdriver.Chrome (“C:\Program Files(x86)\Google\Chrome\chromedriver.exe”)
Browser.maximize_window ()
Browser.get ('https://www.zomato.com / ncr’)
Time.sleep (3)
Username=”test”
Password=”test”
Browser.find_element_by_xpath (“//a [@id=’signin-link]”).click ()
Time.sleep (2)
Browser.find_element_by_xpath (“//a [@id=’ld-email’]”)
a.send_keys (password)
Browser.find_element_by_xpath (“//input [@’ld-submit-global’]”).click ()
Print(‘login successfully’)
Browser. close ()
It is used to start the web browser and need web driver and support browsers,
There are some links to open drivers as follows,
Step1:- Is to install the python first.
Step 2:- Is used to install a favorite driver and allow selenium to interact with favorite browser.
Some downloading links are present as follows:-
Chrome |
https://sites.google.com/a/chromium.org/chromedriver/downloads |
Firefox |
https://github.com/mozilla/geckodriver/releases |
Edge |
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ |
Safari |
https://webkit.org/blog/6900/webdriver-support-in-safari-10/ |
After downloading driver file we perform the following actions as,
Selenium RC |
Selenium Webdriver |
This architecture is complicated as the server need running before starting the test. |
It is simple than RC as it will control the browser from the operating system. |
Rc script execution is slow as it uses JavaScript to interact with RC. |
It is very fast as it interacts with the browser. |
It is a small and simple API. |
It is a complex and large API as compared to RC. |
It is less Object-oriented API |
Purely Objet Oriented API. |
It cannot test mobile application |
It is used to test iphone/Android application. |
After installing the python on the machine we will use selenium with python for testing.
Let us start selenium using python in testing a new project in eclipse.
3.Then click on ‘Next’ button and select the following values:
C:\Python27\Scripts\pip.exe install selenium
The new window will open then continue to PyDev- PYTHONPATH and click on the “External libraries” tab to select the destination: C:\Python27\Lib\site-packages
Enter a Package and Module name (the Package contains the Module):
Then Click on ‘Finish’ button. Now, at this stage we’ll select the ‘Module- Main’:
Start by entering Google search>enter a value>Click ‘Search’:-
From selenium import webdriver
From selenium webdriver.comman.keys import keys
Driver. get (“http://ww.google.com”)
Elem=driver.find_element_by_name (“q”)
Elem.send_keys (“testproject.io”)
Elem.send_keys (Keys.RETURN)
Driver.close ()
The next step is writing the program which test after applying and known as the test automation tool.
Creating new module as selecting the ‘Template’-Unittest.
Enter Google Search>Enter a value>Click ‘Search’
Import unittest
From selenium import webdriver
From selenium.webdriver.comman.keys import Keys
Class Test(unittest.TestCase):
Def setUp (self):
Selft.driver=webdriver.Firefox()
Def teardown (self)
Self.driver.quit ()
Def testName(self):
Driver=self. driver
Driver.get(“http://www.google.com”)
assert”Google”in driver.title
Elem=driver.find_element_by_name (“q”)
Elem.send_keys (“testproject.io”)
Elem.send_keys (Keys.RETURN)
Assert”Kuku” not in driver.page_source
It is not seen,
Import unittest
From selenium import webdriver
From selenium.webdriver.comman.keys import keys
Class Test(Unittest.TestCase):
Def setup (self)
Self.driver=webdriver.Firefox ()
deftearDown (self):
self.driver.quit ()
def testName (self):
driver=self.driver
driver.get (“http://www.google.com”)
assert”Google” in driver.title
elem=driver.find_element_by_name (“q”)
elem.send_keys (“testproject.io”)
elem.send_keys (Keys.RETURN)
assert”Yoni” not in driver.page_source
Output:-