Python_Notes_Daily (10)
Python_Notes_Daily (10)
==================================
Python?
1) Scripting langauge
programming | Scripting
Java, C,C++ | Python, perl, Unix Shell, PHP,javascript
Java -> compile --> Execute
php/python -> Execute
2)Python is High Level Language
3)Python is both Object Oriented and procedure Oreinted
4) Python is dynamic typed langauage
int a=10 , float b=20.2, String s="Nikhil"| a=10, b=20.2, s="Nikhil"
Intrepreter --> Convertor
Each and Every Line of your code will be converted to machine level language
Compiler | Intrepreter
5) Extensible Libraries -> Plug and Play
Modules / Packages
6) Open Source / Platform Independent (OS)
========================================================
Installation:
http://www.python.org
=====================================================
Variables:
print ("welome to python class")
welome to python class
a=10
a
10
b=20.2
b
20.2
type(a)
<class 'int'>
type(b)
<class 'float'>
s="Nikhil"
s
'Nikhil'
type(s)
<class 'str'>
a,b,c = 10,20.2,"Nikhil"
a
10
b
20.2
c
'Nikhil'
a=100
a
100
a=b=c=10
a
10
b
10
c
10
===================================================================================
=========
Identifiers: Rules to define variables, classes, function, etc.,
===================================================================================
=========
Literals : Rules to define the values to identifers
===================================================================================
==============
Data Types:
1. Number - int,float,complex
2. String -
3. List
4. Tuple
5. Set
6. Dictionary
1. String:
a="python"
a='python'
a='''python'''
type(a)
Slicing:
print (a[0])
print (a[1])
a='python'
type(a)
<class 'str'>
a="Python"
a[0]
'P'
a[1]
'y'
a[4]
'o'
a[10]
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
a[10]
IndexError: string index out of range
a[-1]
'n'
a[-2]
'o'
a[-5]
'y'
a[-10]
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
a[-10]
IndexError: string index out of range
a[1:5]
'ytho'
a[1:10]
'ython'
a[0:3]
'Pyt'
a[1: ]
'ython'
a[ : ]
'Python'
a[ : :1]
'Python'
a[ : :2]
'Pto'
a[ : :3]
'Ph'
a[ : :-1]
'nohtyP'
a[ : :-2]
'nhy'
=================================================================
String Methods:
a="Python"
a.capitalize()
-> Capitalize the first character
a.isupper() -> will check all the characters are capital or not
Examples:
a="Python"
a.capitalize()
'Python'
a="python"
a.capitalize()
'Python'
a.islower()
True
b="Python"
b.islower()
False
c="HYDERABAD"
c.isupper()
True
b.isupper()
False
a.upper()
'PYTHON'
d=a.upper()
d
'PYTHON'
a
'python'
c.lower()
'hyderabad'
e=c.lower()
e
'hyderabad'
c
'HYDERABAD'
a.index("h")
3
c.index("A")
5
f="hellohellohihihihihihi"
f.index("hi")
10
f.count("hi")
6
f.count("hello")
2
f.count("h")
8
a.split("h")
['pyt', 'on']
a.split("")
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
a.split("")
ValueError: empty separator
a="hello everyone how r u"
a.split(" ")
['hello', 'everyone', 'how', 'r', 'u']
a
'hello everyone how r u'
"#".join(a)
'h#e#l#l#o# #e#v#e#r#y#o#n#e# #h#o#w# #r# #u'
"|".join(a)
'h|e|l|l|o| |e|v|e|r|y|o|n|e| |h|o|w| |r| |u'
a.isalpha()
False
b.isalpha()
True
c
'HYDERABAD'
c.isalpha()
True
g="hello123"
g.isalpha()
False
k="1234"
k.isdigit()
True
f.isdigit()
False
a.isdigit()
False
p="hello12345"
p.isalnum()
True
b.isalnum()
True
v="hello@#dbdjfjbfhfdsdsfh@#&&&"
v.isalnum()
False
len(g)
8
================================================================
l=[10,20,"Nikhil", 10.2,[1,2]]
print (l)
Number
List String
Set Tuple
Dictionary
Methods:
=======
append --> It is used to add a single element to the last index of list
Examples:
=========
l=[10,20,"Nikhil",20.2]
l[0]
10
l[2]
'Nikhil'
l[2]="Raj"
l
[10, 20, 'Raj', 20.2]
a="Nikhil"
a[2]
'k'
a[2]="h"
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
a[2]="h"
TypeError: 'str' object does not support item assignment
l
[10, 20, 'Raj', 20.2]
l.append("Nikhil")
l
[10, 20, 'Raj', 20.2, 'Nikhil']
l.append(100)
l
[10, 20, 'Raj', 20.2, 'Nikhil', 100]
l.insert(2,30)
l
[10, 20, 30, 'Raj', 20.2, 'Nikhil', 100]
l1=[80,90,200]
l.extend(l1)
l
[10, 20, 30, 'Raj', 20.2, 'Nikhil', 100, 80, 90, 200]
l2=[1,2,2,3,4,5,5,6,3,4,4,3,3,3,3,44,4,4,4,44]
l2.count(2)
2
l2.count(4)
6
len(l)
10
l.sort()
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
l.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
l3=[1,2,3,4,7,6,5,9,12,10,11]
l3.sort()
l3
[1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12]
l4=[1,2,3,4,7,6,5,9,12,10,11]
l5=sorted(l4)
l5
[1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12]
l4
[1, 2, 3, 4, 7, 6, 5, 9, 12, 10, 11]
l3.index(7)
6
l4.index(5)
6
l4
[1, 2, 3, 4, 7, 6, 5, 9, 12, 10, 11]
l4.remove(12)
l4
[1, 2, 3, 4, 7, 6, 5, 9, 10, 11]
del l[2]
del l4[2]
l4
[1, 2, 4, 7, 6, 5, 9, 10, 11]
l4.pop(2)
4
l4
[1, 2, 7, 6, 5, 9, 10, 11]
=====================================================
Tuple:
Examples:
t=( 1,2, "Nikhil", 10,100, 10.2)
t[2]
'Nikhil'
t[2]="Raj"
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
t[2]="Raj"
TypeError: 'tuple' object does not support item assignment
t.count("Nikhil")
1
t.count(100)
1
t.index(100)
4
len(t)
6
====================================================
Set:
Methods:
pop -> pop out some random element as there is no index in set
copy -> it will copy the elelemnts from one set to other set
clear -> it will clear all the data from the set
Examples:
=========
s={1,2,3,4,"Nikhil"}
s.add("Anil")
s
{1, 2, 3, 4, 'Nikhil', 'Anil'}
s.add("Kumar")
s
{1, 2, 3, 4, 'Nikhil', 'Anil', 'Kumar'}
s.update(["som",100,"Durga"])
s
{1, 2, 3, 4, 'Durga', 'Nikhil', 'Kumar', 100, 'som', 'Anil'}
s.remove("som")
s
{1, 2, 3, 4, 'Durga', 'Nikhil', 'Kumar', 100, 'Anil'}
s1=s.copy()
s1
{1, 2, 3, 4, 100, 'Durga', 'Nikhil', 'Anil', 'Kumar'}
s
{1, 2, 3, 4, 'Durga', 'Nikhil', 'Kumar', 100, 'Anil'}
s.clear()
s
set()
l=set()
type(l)
<class 'set'>
l={}
type(l)
<class 'dict'>
====================================================
dictionary:
d={1:"Nikhil", 2:"Raj",3:"Kumar"}
methods:
Examples:
d={1:"Nikhil", 2:"Raj",3:"Kumar",4:"Kumar",5:[1,2]}
d
{1: 'Nikhil', 2: 'Raj', 3: 'Kumar', 4: 'Kumar', 5: [1, 2]}
d.keys()
dict_keys([1, 2, 3, 4, 5])
d.values()
dict_values(['Nikhil', 'Raj', 'Kumar', 'Kumar', [1, 2]])
d.items()
dict_items([(1, 'Nikhil'), (2, 'Raj'), (3, 'Kumar'), (4, 'Kumar'), (5, [1, 2])])
d[6]="pradeep"
d
{1: 'Nikhil', 2: 'Raj', 3: 'Kumar', 4: 'Kumar', 5: [1, 2], 6: 'pradeep'}
del d[2]
d
{1: 'Nikhil', 3: 'Kumar', 4: 'Kumar', 5: [1, 2], 6: 'pradeep'}
d[3]
'Kumar'
d[5]
[1, 2]
d[6]
'pradeep'
d1=d.copy()
d1
{1: 'Nikhil', 3: 'Kumar', 4: 'Kumar', 5: [1, 2], 6: 'pradeep'}
d
{1: 'Nikhil', 3: 'Kumar', 4: 'Kumar', 5: [1, 2], 6: 'pradeep'}
d.clear()
d
{}
============================================================
Conditional Statements :
Example:
a= int(input("Enter the number: "))
if a == 10:
print ("Approved")
elif a == 20:
print ("partial approved")
else:
print ("Denied")
=====================================================================
username=["Nikhil","Kumar","Raj"]
password=[1234,5678,9100]
user= input("Enter the username: ")
passw= int(input("Enter the password:"))
if user in username:
if passw in password:
print ("Approved")
else:
print ("Denied")
else:
print ("Denied")
================================================================
Loops:
l=[1,2,3,4,5,6,7,8,9,10]
for i in l:
print (i)
s={1:2,3:4,5:6,7:8,9:10}
for k,v in s.items():
print (s[k])
for i in range(0,10):
print(i)
for i in range(10):
if i == 5:
print ("Element found")
else:
pass
------------------------------------------------------
While:
a=1
while (a<10):
print (a)
a=a+1
================================================================
control statements:
break --> it is used to break the loop when satisfies the condition
continue --> it is used skip the particular element when condition satisfies
pass --> to just move to next line by just checking syntax
Example:
l=["Nikhil","Kumar","Raj","Varma"]
for i in l:
if i == "Raj":
break
else:
print (i)
for i in range(1,100):
if i == 50:
pass
print (i)
else:
print (i)
for i in range(1,100):
if i == 50:
continue
print (i)
else:
print (i)
========================================================================
Functions: Functions are used for reusability the code
#Call by reference
Ex1:
def fun1(a):#--> Function dec / parameter
print ("Hello Everyone") #] -> Function Body
a=100
print (a)
fun1(500) #-> Function Call/ Argument
jdfjfdsfdshfd
afbfhfhgr
kjhdjhfdhjfdh
hhhhhhhhhhhhhhhhhh
'''
Ex2:
def fun1(a,b):
print ("Hello Everyone")
print (a)
print (b)
fun1([100,12,1000],"Nikhil")
=========================================
#Global variable: Any variable defined outside the function is called global
variable
#Scope of Global varaible is throughout the program
#Local variable: Any variable defined inside the function is called as local
variable
#Scope of it is only inside the function
Ex:
def fun1(a,b,l):
print (a)
print (b)
l=[5,6,7] # local variable
print (l)
l=[1,2,3,4,5] #global variable
fun1("Nikhil","Hyd",l)
print (l)
Keyword arguments:
------------------
Lambda Function:
---------------
Special Function or ananomyous Function
def fun1(a,b):
print (a+b)
fun1("Nikhil","Hyd")
def fun1(a,b):
print (a+b)
def fun2():
print ("This is the second function")
fun1("Nikhil","Hyd")
fun2()
==============================================================
File Handling:
-------------
File with format .txt is text file . Other format is binary file.
File Operations:
open
read/write
close
File Modes:
-----------
r - read mode
w - write mode
r+ - both read and write mode
a - append mode
w+ - both read and write mode
rb - reading binary file
read:
f= open("file2.txt","r")
f1=f.read()
print (f1)
write:
f= open("sample.txt","w")
f.write("jfshjffjhfjhfjhfjfghgjfhfgjhgjgf")
f.close()
append:
f= open("sample.txt","a")
f.write("cvbfbfbbfdsfdhhf")
f.close()
Ex2:
f= open("F://nikhil.txt","w")
f.writelines("Creating new file and writing data\nfjfhjfdhjfdh")
f.close()
f1=open("F://nikhil.txt","r+")
f2=f1.readlines()
for i in f2:
if "new" in i:
print ("Data found")
else:
print ("Data not found")
Ex2:
f1=open("file3.txt","r")
f2=f1.readlines()
print (f2)
print (len(f2))
for i in f2:
if "hello" in i:
print (i)
else:
pass
------------------------------------------------------------------------
Exception Handling:
------------------
Ex:
#NameError
try:
a=100
print (B)
except:
print ("Exception handled")
finally:
print ("This is the finally block")
#IO Exception
try:
f=open("jjfdhhfdhdf.txt","r")
f1=f.read()
print (f1)
except IOError as e:
print ("Exception handled")
print (e)
#IO Exception
try:
f=open("file1.txt","r")
f.write("vdjfhfhfh")
except IOError as e:
print ("Exception handled")
print (e)
#IO Exception
try:
f=open("file1.txt","r")
f.write("vdjfhfhfh")
except BaseException as e:
print ("Exception handled")
print (e)
#Module Exception
try:
import jjdhjsfdsbdsf
except ImportError as e:
print ("Exception handled")
print (e)
import math
import random
print (math.pow(2,3))
print (math.sqrt(25))
print (math.pi)
print (random.randint(1,100))
#Arthmetic Exception
try:
a=1/0
print (a)
except ZeroDivisionError as e:
print ("Exception handled")
print (e)
#Arithmetic Exception
try:
a=1/0
print (a)
except ArithmeticError as e:
print ("Exception handled")
print (e)
#Index Exception
try:
l=[1,2,3,4,5]
print (l[20])
except IndexError as e:
print ("Exception handled")
print (e)
#Raise Exception:
try:
raise Exception ("This is value Error")
except Exception as e:
print ("this is handled")
print (e)
====================================================================
Interview: 2 years in Seleinum with Python
class
object
variable
methods
construct
Example:
class employee:
a=10 # static variable or class variable
def nikhil(self,name,age,salary): #creating a function
b=20 # b is local variable
self.name = name # non static variables
self.age = age
self.salary = salary
print (b)
def display(self):
print ("Employee name: ", self.name)
print ("Employee age: ", self.age)
print ("Employee salary: ", self.salary)
def f1(self,c):
self.c=c
print (self.c)
emp1=employee()
emp1.nikhil("arjun",36,150000)
emp1.display()
emp1.f1(1000)
print (emp1.a)
emp2=employee()
emp2.nikhil("nikhil",36,120000)
emp2.display()
emp2.f1(25000)
print (employee.a)
emp3=employee()
emp3.nikhil("dffhg",222,245556)
emp3.f1(11212)
Ex:
class employee():
a=10 # static variable
def __init__(self,name,age,salary): #creating a function
b=20 # local variable
self.name = name # instance varaible
self.age = age
self.salary= 150000
print (b)
def m1(self):
print ("Hello Everyone")
def display(self,address):
print ("Employee name: ", self.name)
print ("Employee age: ", self.age)
print ("Employee salary: ", self.salary)
self.address="Hyderabad"
print ("Address:", self.address)
def __del__(self):
print ("destructor is called when an object is deleted")
@staticmethod
def m2():
print ("this is a static method")
@classmethod
def m3(cls):
print ("this is class method")
ref1 = employee("nikhil",36,120000)
print (employee.a) #10
ref1.display("Pune")
ref1.m1()
del ref1
ref2=employee("anand",20,25000)
ref2.display("Hyderabad")
ref2.m1()
ref3=employee("rohith",20,25000)
ref3.display("Hyderabad")
ref3.m1()
employee.m2()
employee.m3()
#class variable or static variable: Any variable inside the class and outside the
method
#instance method: Any method with self as first parameter
#local variable: Any variable within the method without self keyword
#self: Default parameter which refer to the current object
# object --> entity of a class
# data memeber --> any variable (Class variable or static ,Instance variable or non
static , Local Variable)
#constructor --> __init__ (Automatically called when an object is created)
#destructor --> __del__ ( Automatically called when object is deleted)
# non static variable :it is defined inside methods with self keyword
#instance varaibles: Any variable with self keyword is called as instance variables
# 3 types variables:
#1. Class Variable or static varaiable: Inside Class and Outside Method ->
#2. Instane Variable or non static variable: Any Variables inside method with self
Keyword ->#self.name
#3. Local variabe: Any Variable inside method without self keyword -->
==============================================================================
OOPS Features:
1. Polymorphism
2. Encapsulation
3. Abstraction
4. Inheritance
method overloading
===================
#METHOD OVERLOADING : SAME METHOD DIFFERENT PARAMETERS
#( NOT POSSIBLE DIRECTLY IN PYTHON)
Example:
class employee1():
def name(self):
print("Harshit is his name")
def salary(self):
print("3000 is his salary")
def age(self):
print("22 is his age")
def salary(self,a):
print ("salary with one parameter")
def salary(self,a,b):
print ("Salary with 2 paramaters")
emp1=employee1()
emp1.name()
emp1.age()
emp1.salary(1000,2000)
================================================================
METHOD OVERRIDING: child class method is overrding the parent class method when the
method
and number of parameters are same
Ex:
class employee1():
def name(self):
print("Harshit is his name")
def salary(self):
print("3000 is his salary")
def age(self):
print("22 is his age")
class employee2(employee1):
def m1(self):
print ("hello")
def salary(self):
print ("New salary is 5000")
emp2=employee2()
emp2.m1()
emp2.salary()
emp2.age()
emp2.name()
emp1=employee1()
emp1.name()
emp1.age()
emp1.salary()
============================================================
Inheritance: Child class Inheriting the properties of parent class
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hireachial Inheritane
1.SimpleInheritance: A
|
B
2. Multiple Inheritance A
|
B (A)
|
C (B,A)
|
D(C,B,A)
3. Multi Level A
|
B(A)
|
C(B)
|
D(C)
4. Hierarchical A
|
B(A)
|
C(A)
|
D(A)
Examples:
==========
Multi-Level :
class employee1():
def name(self):
print("Harshit is his name")
def salary(self):
print("3000 is his salary")
def age(self):
print("22 is his age")
class employee2(employee1):
def m1(self):
print ("hello")
def salary(self):
print ("New salary is 5000")
class employee3(employee2):
def m2(self):
print ("this is the child class of employee2")
def m3(self):
print ("This is multilevel inheritance")
emp3=employee3()
emp3.m2()
emp3.m3()
emp3.salary()
emp3.m1()
emp2=employee2()
emp2.m1()
emp2.salary()
emp2.age()
emp2.name()
emp1=employee1()
emp1.name()
emp1.age()
emp1.salary()
------------------------------------------------------------------
multiple inheritance:
class employee1():
def name(self):
print("Harshit is his name")
def salary(self):
print("3000 is his salary")
def age(self):
print("22 is his age")
class employee2(employee1):
def m1(self):
print (super().salary())
print ("hello")
def salary(self):
print ("New salary is 5000")
class employee3(employee2,employee1):
def m2(self):
print ("this is the child class of employee2")
def m3(self):
print ("This is multilevel inheritance")
emp3=employee3()
emp3.m2()
emp3.m3()
emp3.salary()
emp3.m1()
emp3.age()
emp3.name()
emp2=employee2()
emp2.m1()
emp2.salary()
emp2.age()
emp2.name()
emp1=employee1()
emp1.name()
emp1.age()
emp1.salary()
==========================================================
Hierarchical Inheritance:
class employee1():
def name(self):
print("Harshit is his name")
def salary(self):
print("3000 is his salary")
def age(self):
print("22 is his age")
class employee2(employee1):
def m1(self):
print (super().salary())
print ("hello")
def salary(self):
print ("New salary is 5000")
class employee3(employee1):
def m2(self):
print ("this is the child class of employee2")
def m3(self):
print ("This is multilevel inheritance")
emp3=employee3()
emp3.m2()
emp3.m3()
emp3.salary()
emp3.age()
emp3.name()
emp2=employee2()
emp2.m1()
emp2.salary()
emp2.age()
emp2.name()
emp1=employee1()
emp1.name()
emp1.age()
emp1.salary()
===========================================================
Ex:
def fun1(self):
print (self.__salary)
def m2(self):
print (self.name)
print (self.age)
def __m4(self):
print ("this is a private method")
def m3(self):
print (employee.__a)
employee.__m4(self)
object1 = employee()
object1.f1("nikhil",50,5565555)
object1.fun1()
object1.m2()
object1.m3()
===================================================================
Abstraction:
class childemployee1(employee):
def emp_id(self,ide):
self.ide=ide
print(self.ide)
emp1 = childemployee1()
emp1.emp_id(1000)
#====================================================
Ex2:
@abstractmethod
def emp_id(self,ide):
pass
def f1(self):
print ("Non abstract method")
class childemployee1(employee):
def emp_id(self,ide):
self.ide=ide
print(self.ide)
def m1(self):
print ("Hello")
class childEmployee2(employee):
def emp_id(self,ide):
self.ide=ide
print(self.ide)
def m2(self):
print ("Bye")
emp1 = childemployee1()
emp1.emp_id(1000)
emp2=childEmployee2()
emp2.m2()
emp2. emp_id(2000)
emp2.f1()
================================================================================
Selenium Day1:
Selenium Introduction:
Selenium is a open soure Suite of Testing Tools used for mainly Functional Testing
of webapplications
Selenium supports multiple languages ( Ruby- 5, C# - 5, Python - 40 , Java -
50 ,Java Script,Perl etc.,)
Devloper (40 ), Data scientist (30 ), Automation (30) --> naukari , python,
selenium,API Testing,selenium webdriver
Selenium supports multiple os( windows, Linux, Mac)
Selenium supports multiple browsers ( Chrome, FireFox, Safari) -> Parellel Testing
Limitations:
1. Cannot automate Desktop Based Application
( Auto IT/ Pyautogui ) has to be used to work on Windows ( Desktop ) Applications
2. Cannot generate Reports - TestNg and Extent Reports / HTML and Allure Reports
3. Proritizing and Organize-> pytest , unit, Robot
---------------------------------------------------------------------------
HTML, Angular --> Selenium
==============================================================================
Selenium has 4 components :
Selenium IDE:
IDE (Integrated Development Environment) is a Firefox plugin.
It is one of the simplest frameworks in the Selenium Suite.
It allows us to record and playback the scripts
---------------------------------------------------------
1. Install Selenium
-------------------------------------------------
Selenium Day1:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
service_obj=Service("C://Users//admin//Desktop//chromedriver.exe")
driver=webdriver.Chrome(service=service_obj)
driver.get("http://www.durgasoft.com/registration.asp")
driver.maximize_window()
driver.find_element(By.ID,"name").send_keys("sindhusha")
driver.find_element(By.ID,"ph_no").send_keys("4545454454")
driver.find_element(By.ID,"email").send_keys("[email protected]")
driver.close()
=================================================================
Locators:
Locators:
id --> find_element(By.ID,"value")
class name -->
name --> find_element(By.NAME,"value")
xpath --> find_element(By.XPATH,"//input[@id='name']")
tagname --> find_element(By.TAGNAME,"a")
link text
partial link text
css selector
===========================================================================
Ex:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
service_obj=Service("C://Users//admin//Desktop//chromedriver.exe")
driver=webdriver.Chrome(service=service_obj)
driver.get("http://www.durgasoft.com/registration.asp")
driver.maximize_window()
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Nikhil Raj")
driver.find_element(By.XPATH,"//input[@name='ph_no']").send_keys("9552225444")
driver.find_element(By.XPATH,"(//input[@class='nor-text'])
[3]").send_keys("[email protected]")
driver.find_element(By.ID,"name").clear()
time.sleep(3)
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Kumar")
driver.close()
====================================================================
Ex:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
service_obj=Service("C://Users//admin//Desktop//chromedriver.exe")
driver=webdriver.Chrome(service=service_obj)
driver.get("http://www.durgasoft.com/registration.asp")
driver.maximize_window()
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Deepak")
driver.find_element(By.XPATH,"//input[@name='ph_no']").send_keys("9552225444")
driver.find_element(By.XPATH,"(//input[@class='nor-text'])
[3]").send_keys("[email protected]")
driver.find_element(By.ID,"name").clear()
time.sleep(3)
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Kumar")
driver.find_element(By.XPATH,"//input[@value='Student']").click()
driver.find_element(By.XPATH,"//input[@value='Un-Employed/Job Seeker']").click()
time.sleep(3)
driver.close()
========================================================================
Ex2:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
service_obj=Service("C://Users//admin//Desktop//chromedriver.exe")
driver=webdriver.Chrome(service=service_obj)
driver.get("http://www.durgasoft.com/registration.asp")
driver.maximize_window()
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Deepak")
driver.find_element(By.XPATH,"//input[@name='ph_no']").send_keys("9552225444")
driver.find_element(By.XPATH,"(//input[@class='nor-text'])
[3]").send_keys("[email protected]")
driver.find_element(By.ID,"name").clear()
time.sleep(3)
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Ramesh")
driver.find_element(By.XPATH,"//input[@value='Student']").click()
driver.find_element(By.XPATH,"//input[@value='Employed']").click()
time.sleep(3)
driver.find_element(By.XPATH,"//input[@id='CORE_JAVA']").click()
time.sleep(2)
driver.close()
=======================================================================
#WebDriver Methods:
driver.get()
driver.maximize_window()
driver.find_element()
driver.find_elements()
driver.close()
driver.implicitly_wait()
driver.refresh()
driver.back()
driver.forward()
driver.quit()
driver.switch_to_window()
driver.switch_to_alert()
driver.switch_to_frame()
driver.title()
driver.current_url()
driver.execute_script()
driver.window_handles()
=========================================================================
Ex: refresh(), forward(), back(), current_url(), title
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
service_obj=Service("C://Users//admin//Desktop//chromedriver.exe")
driver=webdriver.Chrome(service=service_obj)
driver.get("http://www.durgasoft.com/registration.asp")
driver.maximize_window()
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Deepak")
driver.find_element(By.XPATH,"//input[@name='ph_no']").send_keys("9552225444")
driver.find_element(By.XPATH,"(//input[@class='nor-text'])
[3]").send_keys("[email protected]")
driver.find_element(By.ID,"name").clear()
time.sleep(3)
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Ramesh")
driver.find_element(By.XPATH,"//input[@value='Student']").click()
driver.find_element(By.XPATH,"//input[@value='Employed']").click()
time.sleep(3)
driver.find_element(By.XPATH,"//input[@id='CORE_JAVA']").click()
driver.find_element(By.XPATH,"//input[@id='C-Language']").click()
time.sleep(2)
print (driver.title)
print (driver.current_url)
driver.refresh()
driver.back()
driver.forward()
driver.close()
=========================================================================
Assertions:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
service_obj=Service("C://Users//admin//Desktop//chromedriver.exe")
driver=webdriver.Chrome(service=service_obj)
driver.get("http://www.durgasoft.com/registration.asp")
driver.maximize_window()
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Deepak")
driver.find_element(By.XPATH,"//input[@name='ph_no']").send_keys("9552225444")
driver.find_element(By.XPATH,"(//input[@class='nor-text'])
[3]").send_keys("[email protected]")
driver.find_element(By.XPATH,"//input[@value='Student']").click()
driver.find_element(By.XPATH,"//input[@id='CORE_JAVA']").click()
time.sleep(2)
print (driver.title)
assert "DURGA SOFTWARE SOLUTIONS" in driver.title
print (driver.current_url)
assert "http://www.durgasoft.com/registration.asp" in driver.current_url
time.sleep(5)
driver.close()
==============================================================================
Implicit_wait(): Implicit wait directs the selenium webdriver to wait for certain
measure of time before throwing an exception.It is applicable to every webelement
---------------------------------------------------------------------------------
Ex:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
service_obj=Service("C://Users//admin//Desktop//chromedriver.exe")
driver=webdriver.Chrome(service=service_obj)
driver.get("http://www.durgasoft.com/registration.asp")
driver.implicitly_wait(20)
driver.maximize_window()
driver.find_element(By.XPATH,"//input[@id='name']").send_keys("Deepak")
driver.find_element(By.XPATH,"//input[@name='ph_no']").send_keys("9552225444")
driver.find_element(By.XPATH,"(//input[@class='nor-text'])
[3]").send_keys("[email protected]")
a=driver.find_element(By.XPATH,"//input[@value='Student']")
print (a.is_selected()) #false
a.click()
print (a.is_enabled()) #true
print (a.is_displayed()) #true
print (a.is_selected()) #true
driver.find_element(By.XPATH,"//input[@id='CORE_JAVA']").click()
c=driver.find_elements(By.TAG_NAME,"a")
print (len(c))
for i in c:
print (i.text)
a=driver.find_element(By.XPATH,"//a[@href='/corejava.asp']")
print (a.text)
time.sleep(2)
print (driver.title)
assert "DURGA SOFTWARE SOLUTIONS" in driver.title
print (driver.current_url)
assert "http://www.durgasoft.com/registration.asp" in driver.current_url
time.sleep(5)
driver.close()
==============================================================================