Skip to main content

Python Selenium Webdriver Function

 #******************************************************************************************************


#Reference Links
    #https://selenium-python.readthedocs.io/
    #https://selenium-python.readthedocs.io/api.html

#*******************************************************************************************************

#Import Selenium Webdriver Common

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui as ui
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import ElementNotVisibleException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.keys import Keys
from distutils.dir_util import copy_tree
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import os
import time
import pymsgbox
#-------------------------------------------------------------------------------------------------------
    #Class Name - ClsSelenium
    #Desc - This class will helps to do the automation using Selenium.
#-------------------------------------------------------------------------------------------------------
class ClsSelenium():    
#-------------------------------------------------------------------------------------------------------
    #Function Name - driver_initialize
    #Desciption - Open the Chrome driver(browser) | Change the DownloadsPath |
    #Parameter Name - DriverPath, DownloadsPath
    #Function Return - ObjBrowser
#-------------------------------------------------------------------------------------------------------    
    def driver_initialize(self, DriverPath, DownloadsPath):
        chromeoptions =webdriver.ChromeOptions()
        prefs = {"download.default_directory": DownloadsPath}
        chromeoptions.add_experimental_option("prefs",prefs)
        chromedriver= DriverPath
        ObjBrowser= webdriver.Chrome(chrome_options=chromeoptions, executable_path= chromedriver)
        ObjBrowser.maximize_window()
        return ObjBrowser
#------------------------------------------------------------------------------------------------------  
    #Function Name - UpdateText_XPATH
    #Desc- Update the String into TextBox Element
    #Parameter Name - ObjBrowser, IntDelay, StrXpath, StrInputText
    #Function Return -N/A
#-------------------------------------------------------------------------------------------------------        
    def UpdateText_XPATH(self, ObjBrowser, IntDelay, StrXpath, StrInputText):
        try:
            element = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, StrXpath)))
            element.send_keys(StrInputText)
        except:
            msg=str("Unable to enter the text -: " + StrInputText)
            print(msg)
#------------------------------------------------------------------------------------------------------  
    #Function Name - UpdateText_Element
    #Desc- Update the String into TextBox by received the Element
    #Parameter Name - ObjBrowser, IntDelay, ObjElement, StrInputText
    #Function Return -N/A
#-------------------------------------------------------------------------------------------------------            
    def UpdateText_Element(self, ObjBrowser, IntDelay, ObjElement, StrInputText):
        try:
            ObjElement.send_keys(StrInputText)
        except:
            msg=str("Unable to enter the text -: " + StrInputText)
            print(msg)          
#------------------------------------------------------------------------------------------------------  
    #Function Name - Click_XPATH
    #Desc- Click the object using XPATH
    #Parameter Name - ObjBrowser, IntDelay, StrXpath
    #Function Return -N/A
#-------------------------------------------------------------------------------------------------------              
    def Click_XPATH(self, ObjBrowser, IntDelay, StrXpath):
        try:
            element = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, StrXpath)))
            element.click()
        except:
            msg=str("Unable to click -: " + StrXpath)
            print(msg)
#------------------------------------------------------------------------------------------------------  
    #Function Name - Click_Element
    #Desc- Click the element  by received the ObjElement
    #Parameter Name -  ObjBrowser, IntDelay, ObjElement
    #Function Return -N/A
#-------------------------------------------------------------------------------------------------------            
    def Click_Element(self, ObjBrowser, IntDelay, ObjElement):
        try:
            ObjElement.click()
        except:
            msg=str("Unable to click -: ")
            print(msg)    
#-------------------------------------------------------------------------------------------------------  
    #Function Name - Dropdown
    #Desc- Select the Dropdown by XPATH and Choose the option using option_StrXpath
    #Parameter Name -   ObjBrowser, IntDelay, dropdown_StrXpath, option_StrXpath
    #Function Return -N/A
#-------------------------------------------------------------------------------------------------------                  
    def Dropdown(self, ObjBrowser, IntDelay, dropdown_StrXpath, option_StrXpath):
        try:
            WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, dropdown_StrXpath))).click()
            WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, option_StrXpath))).click()
        except:
            msg=str("Unable to find the dropdown -: " + dropdown_StrXpath)
            pymsgbox.alert(msg,"ATTENTION",button = "OK")
#-------------------------------------------------------------------------------------------------------
    #Function Name - SwitchIframe
    #Desc- Change Focus to Iframe
    #Parameter Name -   ObjBrowser, IntDelay, StrXpath
    #Function Return - ObjBrowser
#-------------------------------------------------------------------------------------------------------            
    def SwitchIframe(self, ObjBrowser, IntDelay, StrXpath):
        try:
            frame = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, StrXpath)))
            ObjBrowser.switch_to_default_content()
            ObjBrowser.switch_to.frame(frame)
            return ObjBrowser
        except:
            msg=str("Unable to switch the iframe -: " + StrXpath)
            print(msg)
#-------------------------------------------------------------------------------------------------------
    #Function Name - GetText
    #Desc- Get text from web element
    #Parameter Name -  ObjBrowser, IntDelay, StrXpath
    #Function Return - outputText
#-------------------------------------------------------------------------------------------------------              
    def GetText(self, ObjBrowser, IntDelay, StrXpath):
        try:
            outputText = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, StrXpath))).text
            return outputText
        except:
            msg=str("Unable to get the text -: " + StrXpath)
            pymsgbox.alert(msg,"ATTENTION",button = "OK")
#-------------------------------------------------------------------------------------------------------
    #Function Name - GetAttributeText_XPATH
    #Desc- Get attribute value from web element using xpath
    #Parameter Name -  ObjBrowser, IntDelay, StrXpath, StrAttribute_name
    #Function Return - outputText
#-------------------------------------------------------------------------------------------------------                    
    def GetAttributeText_XPATH(self, ObjBrowser, IntDelay, StrXpath, StrAttribute_name):
        try:
            outputText = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, StrXpath))).get_attribute(StrAttribute_name)
            return outputText
        except:
            msg=str("Unable to get the attribute text -: " + StrXpath)
            print(msg)
#-------------------------------------------------------------------------------------------------------
    #Function Name - GetAttributeText_ObjElement
    #Desc- Get attribute value from web element using ObjElement
    #Parameter Name -  ObjBrowser, IntDelay, ObjElement, StrAttribute_name
    #Function Return - outputText
#-------------------------------------------------------------------------------------------------------
    def GetAttributeText_ObjElement(self, ObjBrowser, IntDelay, ObjElement, StrAttribute_name):
        try:
            outputText = ObjElement.get_attribute(StrAttribute_name)
            return outputText
        except:
            msg=str("Unable to get the attribute text -: ")
            print(msg)
#-------------------------------------------------------------------------------------------------------
    #Function Name - AlertAccept
    #Desc- Accept Alert
    #Parameter Name -  ObjBrowser, IntDelay
    #Function Return - N/A
#-------------------------------------------------------------------------------------------------------
    def AlertAccept(self, ObjBrowser,IntDelay):
        while True:
            try:
                alert_switch = ObjBrowser.switch_to_alert()
                alert_switch.accept()
                break
            except:
                print("waiting for Alert and Switch to")  
#-------------------------------------------------------------------------------------------------------
    #Function Name - AlertDismiss
    #Desc- Dismiss Alert
    #Parameter Name -  ObjBrowser, IntDelay
    #Function Return - N/A
#-------------------------------------------------------------------------------------------------------                
    def AlertDismiss(self, ObjBrowser,IntDelay):
        while True:
            try:
                alert_switch = ObjBrowser.switch_to_alert()
                alert_switch.dismiss()
                break
            except:
                print("waiting for Alert and Switch to")  
#-------------------------------------------------------------------------------------------------------
    #Function Name - ExecuteJavascript
    #Desc- Execute JavaScript fucntion on element action.
    #Parameter Name -  ObjBrowser, IntDelay, StrXpath, StrScript
    #Function Return - N/A
#-------------------------------------------------------------------------------------------------------                
    def ExecuteJavascript(self, ObjBrowser, IntDelay, StrXpath, StrScript):
        try:
            element = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, StrXpath)))
            ObjBrowser.execute_script(StrScript, element)
        except:
            print('Unable to find element!',' StrXpath :-', StrXpath)
#-------------------------------------------------------------------------------------------------------
    #Function Name - GetAll_Obj_By_Xpath
    #Desc- Get Multiple elements which has similar Tags and property in (Xpath)
    #Parameter Name -  ObjBrowser, IntDelay, StrXpath
    #Function Return - element
#-------------------------------------------------------------------------------------------------------            
    def GetAll_Obj_By_Xpath(self, ObjBrowser, IntDelay, StrXpath):
        try:
            element = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_all_elements_located((By.XPATH, StrXpath)))
            return element
        except:
            print('Unable to find element!',' StrXpath :-', StrXpath)
#-------------------------------------------------------------------------------------------------------
    #Function Name - GetAll_Obj_By_CSS
    #Desc- Get Multiple elements which has similar Tags and property in (CSS)
    #Parameter Name -  ObjBrowser, IntDelay, StrCssSelector
    #Function Return - element
#-------------------------------------------------------------------------------------------------------            
    def GetAll_Obj_By_CSS(self, ObjBrowser, IntDelay, StrCssSelector):
        try:
            element = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, StrCssSelector)))
            return element
        except:
            print('Unable to find element!',' CSS_SELECTOR :-', StrCssSelector)
#-------------------------------------------------------------------------------------------------------
    #Function Name - Get_Obj_By_StrXpath
    #Desc- Get object by using XPATH
    #Parameter Name -  ObjBrowser, IntDelay, StrXpath
    #Function Return - element
#-------------------------------------------------------------------------------------------------------            
    def Get_Obj_By_StrXpath(self, ObjBrowser, IntDelay, StrXpath):
        try:
            element = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, StrXpath)))
            return element
        except:
            print('Unable to find element!',' StrXpath :-', StrXpath)
#-------------------------------------------------------------------------------------------------------
    #Function Name - Get_Obj_By_CSS
    #Desc- Get object by using CSS
    #Parameter Name -  ObjBrowser, IntDelay, StrCssSelector
    #Function Return - element
#-------------------------------------------------------------------------------------------------------            
    def Get_Obj_By_CSS(self, ObjBrowser, IntDelay, StrCssSelector):
        try:
            element = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.CSS_SELECTOR, StrCssSelector)))
            return element
        except:
            print('Unable to find element!',' CSS_SELECTOR :-', StrCssSelector)
#-------------------------------------------------------------------------------------------------------
    #Function Name - SendKeys
    #Desc- Do keyboard actions
    #Parameter Name -  ObjBrowser, IntDelay, StrXpath, StrKey
    #Function Return - N/A
#-------------------------------------------------------------------------------------------------------            
    def SendKeys(self, ObjBrowser, IntDelay, StrXpath, StrKey):
        try:
            element = WebDriverWait(ObjBrowser, IntDelay).until(EC.presence_of_element_located((By.XPATH, StrXpath)))
            element.send_keys(StrKey)
        except:
            print('Unable to locate element!')
#-------------------------------------------------------------------------------------------------------
    #Function Name - Open_New_Window
    #Desc- Open Hyper Text Reference(href) in new window
    #Parameter Name -  ObjBrowser, Str_href
    #Function Return - current_window
#-------------------------------------------------------------------------------------------------------
    def Open_New_Window(self, ObjBrowser, Str_href):
        ObjBrowser.execute_script('window.open(arguments[0]);', Str_href)
        current_window = ObjBrowser.current_window_handle
        new_window = [window for window in ObjBrowser.window_handles if window != current_window][0]
        ObjBrowser.switch_to.window(new_window)
        return current_window
#-------------------------------------------------------------------------------------------------------
    #Function Name - Close_New_Window
    #Desc- Close Hyper Text Reference(href) in new window
    #Parameter Name -  ObjBrowser, ObjNew_window
    #Function Return - N/A
#-------------------------------------------------------------------------------------------------------        
    def Close_New_Window(self, ObjBrowser, ObjNew_window):
        ObjBrowser.close()
        ObjBrowser.switch_to.window(ObjNew_window)
#-------------------------------------------------------------------------------------------------------
    #Function Name - GoTo_PageEnd
    #Desc- Goto end of the webpage
    #Parameter Name -  ObjBrowser
    #Function Return - N/A
#-------------------------------------------------------------------------------------------------------        
    def GoTo_PageEnd(self, ObjBrowser):
        lenOfPage = ObjBrowser.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
        match=False
        while(match==False):
                lastCount = lenOfPage
                time.sleep(3)
                lenOfPage = ObjBrowser.execute_script("window.scrollTo(0, document.body.scrollHeight);var lenOfPage=document.body.scrollHeight;return lenOfPage;")
                if lastCount==lenOfPage:
                    match=True
#-------------------------------------------------------------------------------------------------------
    #Function Name - Get_URL
    #Desc- Get current URL
    #Parameter Name -  ObjBrowser
    #Function Return - N/A
#-------------------------------------------------------------------------------------------------------        
    def Get_URL(self, ObjBrowser):
        return ObjBrowser.current_url

Comments

Popular posts from this blog

Python - PyQt5 set Background image

import sys from PyQt5.QtCore import QSize from PyQt5.QtGui import QImage, QPalette, QBrush from PyQt5.QtWidgets import * class MainWindow ( QWidget ): def __init__ ( self ): QWidget. __init__ ( self ) self .setGeometry( 100 , 100 , 300 , 200 ) oImage = QImage( "test.jpg" ) sImage = oImage.scaled(QSize( 300 , 200 )) # resize Image to widgets size palette = QPalette() palette.setBrush( 10 , QBrush(sImage)) # 10 = Windowrole self .setPalette(palette) self .show() if __name__ == "__main__" : app = QApplication(sys.argv) oMainwindow = MainWindow() sys.exit(app.exec_())

pymsgbox - python package for message box

#This tutorial for to get a confirmation from the user by promting window #Python package "pymsgbox" needs to be install before run this below code #To install "pymsgbox" package go to command prompt and type in "PIP3 INSTALL pymsgbox" import pymsgbox #Displays a message box with OK and Cancel buttons. Number and text of buttons can be customized. Returns the text of the button clicked on. replyV = pymsgbox.confirm( text = 'Please login your application and click on on okay' , title = 'Login Confirmation' , buttons =[ 'OK' , 'Cancel' ]) print (replyV) #Displays a simple message box with text and a single OK button. Returns the text of the button clicked on. replyV = pymsgbox.alert( text = '' , title = '' , button = 'OK' ) print (replyV) #Displays a message box with text input, and OK & Cancel buttons. Returns the text entered, or None if Cancel was clicked. replyV1 = pyms