Selenium python tutorial for beginner

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.

Web scraping using selenium python tutorial

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,

  1. Enter the content structures
  2. Permit it tap buttons
  3. Check everything is”OK”

Install selenium Python tutorial for beginner:-

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.

The types of web drivers are as:-

  1. FirefoxDriver
  2. ChromeDriver
  3. RemoteWebDriver

Selenium will support python and utilize selenium with testing:-

  1. It is an easy language as compared to other language.
  2. API empowers and connects with the browser through selenium.
  3. It sends the python command to browsers in their design.

Use of python over java in selenium:-

  1. Java runs slow as compared to python programs.
  2. They use braces to start and end blocks while the python use indentation.
  3. The java language employs static type while python is dynamic type.
  4. It is simple than java.

List of commands with syntax in web Driver:-

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.

Web driver:-

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.

Program to login on the website for example zomato:-

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 ()

Web driver:-

It is used to start the web browser and need web driver and support browsers,

  1. Chrome
  2. Firefox
  3. Internet explorer
  4. Safari
  5. Opera

There are some links to open drivers as follows,

Chrome:

https://sites.google.com/a/chromium.org/chromedriver/downloads

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/

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/

Step 3:-

After downloading driver file we perform the following actions as,

  1. Extracting the driver file and put it into the folder.
  2. Then copy folder path in which file is present.
  3. Then add folder path as enviourement variable that you have mention in the path.

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.

Installation of selenium python tutorial for beginner :-

Let us start selenium using python in testing a new project in eclipse.

  1. After opening eclipse as.file>new, select PyDev project.

selenium python tutorial for beginner

  1. Select ‘PyDev project’:

install selenium python

3.Then click on ‘Next’ button and select the following values:
selenium python tutorial

  1. After this click on ‘Finish’ and confirm the message:

selenium python tutorial for beginner

  1. Look the project as the following in Package Explorer of Eclipse:

selenium using python tutorial

  1. To install Selenium’s library in Python project, open a command line and enter the following line:

C:\Python27\Scripts\pip.exe install selenium

  1. After installation, we will see the message “Selenium successfully installed”:

selenium in python tutorial

  1. Now assign the Selenium libraries that are downloaded, to the new project which is opened.
  2. Then right click on the project in Package Explorer>Properties.

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
selenium python pycharm tutorial

  1. First piece of code: return once again to Eclipse and open a new module in Python, then click File>New>PyDev

new module in Python
Enter a Package and Module name (the Package contains the Module):
Package contains the Module
Then Click on ‘Finish’ button. Now, at this stage we’ll select the ‘Module- Main’:
python tutorial for automation testing
Start by entering Google search>enter a value>Click ‘Search’:-

Program:-

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 automatic test in selenium:-

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.

automatic test in selenium selenium import webdriver
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,
Making test fail by changing the assert value

Making test fail by changing the assert value in end of test:-

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:-
selenium import webdriver

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India