Skip to main content

Python automation with selenium.

Automate with selenium

In this blog we are going to see how to automate some task with selenium.

Selenium is an opensource framework which is used for testing web application by scrapping into the web site.

To apply selenium in python for testing an application we need to follow some step.

So in these this blog we are going to explain how to use selenium to go to the google and search for shotwithcode blog and read the blog.

Step1: using pip in python we have to install selenium "pip install selenium"

Step2: We need a web driver in which it will run the browser and tell the browser what to do.

So if we use chrome we need to download chrome driver else if we use other browser then we need to download the web driver for that browser

You can download the Chrome web driver here https://chromedriver.chromium.org/downloads

Remember to download the chrome driver related to the chrome version we have

Step3:After download the driver extract the file and save into a location which is easy to access i prefer to save in C drive

Step4:So now we have installed everything needed for our  project so now we need to right code to perform the task

Code:

  1. from selenium import webdriver
  2. PATH = "C:/chromedriver"
  3. driver = webdriver.Chrome(PATH)
  4. driver.get('https://www.google.com')
  5. search = driver.find_element_by_name('q')
  6. search.send_keys('shotwithcode.blogspot.com')
  7. clic_k = driver.find_element_by_xpath('/html/body/div[2]/div[2]/form/div[2]/div[1]/div[3]/center/input[1]')
  8. click_k.click()
  9. next = driver.find_element_by_xpath('/html/body/div[7]/div[2]/div[10]/div[1]/div[2]/div/div[2]/div[2]/div/div/div[1]/div/div[1]/a/div/cite')
  10. next.click()
  11. read = driver.find_element_by_xpath(' /html/body/div[1]/div[2]/div[1]/div/div/main/div/div[1]/div/div/div[2]/div/div[3]/div[2]/a')
  12. read.click() 

Let us talk about the line of code we wrote

So PATH is the path of the chrome driver which you have store the chrome driver

Line3: It open the chrome browser

Line4: After opening the browser it will go to google.com

Line5: Here we use find_element_by_name to get the input field we can get this by inspecting the element

Line6: send_keys actually filling the input field

Line7: It is to find the element here i use xpath because xpath will give you the actual element

Line8: Click is use to click the button or link 

Try this code in your system and let me know about your code, you can do what ever you want whit this code.

Conclusion:

We saw that we search the shotwithcode blogspot and read the blog with out doing it manually instead python selenium did it for us.This is one of the method to test the website which we have built, if we have built any website we can test the website with selenium framework.

Let me know what you think about this project, i always welcome to any suggestion or feedback.


Comments

Popular posts from this blog

I built a Django weather app using openweatherapp API

 I built a Django weather app using openweatherapp API.  Yes i built this simple weather app using openweatherapp API, it was fun an easy to built with django. How do i built it? I install the module needed for this project, the only module we need is a request module for python since i am using python to built this project. After installing and setting up the project with django i download a template and start with creating the views for the project. Here how goes: 1.First go to openweather app website : https://openweathermap.org/ 2.Signup for an account so that the openweather app will send you an api key 3.After singup with an account go to API and select the API you want to use 4.In an API doc it will attach a link which look like this api.openweathermap.org/data/2.5/weather?q= {city name} &appid= {API key} So we need to replace the {API key} with our own API key. 5.That it for the API so in order to make a request or to call the API in python we use a request module...

Build a tic tac toe game using python

 Hello, In this blog i am going to teach you how to create a tic tac toe game using python. If you know the basic of python like function, loops, conditional, list then you are good to go. In this project we are going to create it with a simple format of python without using any import or any object oriented programming. Requirement: 1.Board 2.Player(Computer and a user) 3.Winner Python fundamental needed: 1.Variable 2.List 3.If elif else 4.While 5.Function Here we are going to start A tic tac toe game is a game which has three rows and three column so we need a list which is the size of 3X3 which is nine to store the empty dash for the board Line 4: We assign a game_running to True which mean that the game is still runing when we first start the game. Line 6: Defining a function to display the board when ever we call the display_board function. Line 18: When we first start the game the board will display as empty with dash in it. Output is in line 8 Line 19:  We set the curre...

I built a heathcare website from scratch with html,css,javascript and python(Django) framework.

I built a heathcare website from scratch with html,css,javascript and python(Django) framework.  Healthcare Website Here how it goes from the beginning of the process of making the website. Back then in the month of Jan i learn complete in depth about HTML and CSS an a little bit of java script by designing a website everyday until i feel a bit confident about my front end part i design a website every single day for the whole month of Jan. By the end of Jan i am a little bit busy with my academics and then lock down started so i told my self what can i do with python language because i was quite comfortable with python but at that time i'm not interest into machine learning or AI, I was more into web development so i told myself how can i use my python skill into real world, so i did some research and i found out that Django is the web framework of python they also suggest that for a beginner to use flask because Django is quite confusing but i just pick Django with out thinking a...