Selenium is a python library which
is used to scrape web page. Selenium python provides application programming
interface (API) to implement functional test with the help web driver. Selenium
web driver functionalities accessed through selenium python (API). Basically it
is used to automate website. It reduce human affect.
Selenium
with python
Selenium is coded in python
language. It is use to scrape web page or page. Selenium is consist multiple
classes or method which proved helpful in scraping web page. We can access web
page by its URL and page element.
Selenium python web
driver
Web driver used to automate browser.
Web driver are automation APIs use to control the behavior of browser and run
test. Selenium is a building which contain multiple rooms (tools and libraries)
that enable browser to automation web browser. Web driver are designed more
simple and concise programing.
Selenium python web scraping
Selenium is python library which is
used to scrape web page. We can access web page and we can also send data to
web page through python.
Example
Procedure:
Step 1
Create a login
page with (user id, password and login) controls.
OR
We can also use
online page like Facebook login page.
Step 2
Install
python. If already you installed then ok
Install
Selenium library
Install
web driver for instance chrome web driver.
Implementation:
I will show you how you will
automate both type page like online (Facebook) and local html from.
First of all I will show you how we
can automate local page.
HTML Code For Local Page
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "UTF-8">
<meta name = "viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv = "X-UA-Compatible" content = "ie=edge">
<meta name = "Description" content = "Enter your description here"/>
<link rel = "stylesheet"
href = "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.0/css/bootstrap.min.css">
<link rel = "stylesheet" href = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<link rel = "stylesheet" href = "style.css">
<title> Automatic Login Process </title>
</head>
<body>
<div class = "card">
<div class = "card-body">
<h3 class = "card-title text-center">Automatic Login Process</h3>
</div>
</div>
<div class = "card mx-5 my-1">
<div class = "card-body">
<div class = "mb-3">
<label for = "" class="form-label"></label>
<input id = "enrolment" type="text" class="form-control" aria-describedby="helpId" placeholder = "Enter Enrollment">
<small id = "helpId" class = "form-text text-muted">Please fill enrolment box. </small>
</div>
<div class = "mb-3">
<label for = "" class="form-label"></label>
<input id = "password" type="password" class="form-control" aria-describedby = "helpId" placeholder="Enter Password">
<small id = "helpId" class = "form-text text-muted">Please fill password box. </small>
</div>
<div class = "form-group">
<button id = "login" type="submit" class="btn btn-success"> Login </button>
</div>
</div>
</div>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/popper.js/2.9.2/umd/popper.min.js"></script>
<script src = "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.1.0/js/bootstrap.min.js"></script>
<script>
var button = document.getElementById("login");
button.onclick = function(){
alert("Login has been done successfully")
}
</script>
</body>
</html>
Output
Selenium Python Code
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
def install_chrome():
chrome = (ChromeDriverManager().install())
dr = webdriver.Chrome(chrome)
return dr
def open_page(url):
dr = webdriver.Chrome()
dr.get(url)
return dr;
def get_element_by_Id(dr, id):
return dr.find_element_by_id(id)
driver = install_chrome()
driver = open_page('file:///C:/Users/Nouman%20Shoukat/Desktop/Py/selenium/index.html')
enrolment = get_element_by_Id(driver,'enrolment')
password = get_element_by_Id(driver,'password')
login = get_element_by_Id(driver,'login')
enrolment.send_keys("1234")
password.send_keys("459")
login.click()
alert = driver.switch_to_alert()
time.sleep(5)
alert.accept()
Local Login Form Automation |
in above python code you see three
method
install_chrome()
when this
function is called then a chrome is installed and return a object of web driver
chrome.
open_page(url)
when this
function is called then provided url page loaded and return a object of web
driver.
get_element_by_id(driver, id)
this
method used to access web page control by id for instance text box, button and etc.
this method
takes to arguments driver which is object of web driver while id is string
object while hold html tag ids.
Send_keys(str)
This method use
when we want to send text to web page. This method is access by object of
select tag or control from html tag.
Click()
This method use
when we want to click on html element. For this purpose we firstly access html
element for example we access a button element or anchor element then we
perform click action.
switch_to.alert
This is used to
access popup alert dialog box. We can accept or dismiss alert box.
To accept alert
box we use,
Object_name.accept()
for example alert,accept()
To dismiss alert
box we use,
Object_name.dismiss()
for example alert.dismiss()
Alert.text
This
is use to see prompt message in terminal.
Facebook Login Process
Simple set Facebook login page path
like this
driver = open_page(FACEBOOK_LOGIN_PAGE_PATH)
OR
Driver.get(FACEBOOK_LOGIN_PAGE_PATH)
And open inspect view of Facebook
login page and find text box ids and login buttion id
#Facebook login from control ids
email_and_phone_no_ID = 'email'
password_ID ='pass'
login_button_ID = 'u_0_d_Ks'
# Access Facebook login form controll eg email/Phone No text box, password text box and login button
email_text = get_element_by_Id(email_and_phone_no_ID)
password_textbox = get_element_by_Id(password_ID)
login_button = get_element_by_Id(login_button_ID)
#Asign your email or phone no and password
email_text.send_keys("YOUR_EMAIL/PHONE_NO")
password_textbox.send_keys("YOUR_PASSWORD")
#click login button
login_button.click()
0 Comments