Powered By Blogger

Search Here!

Friday, July 31, 2015

Handling ! Page Load Events In Python Selenium !

Handling ! Page Load Events In Python Selenium.

from selenium.common.exceptions import InvalidElementStateException, StaleElementReferenceException
from selenium.webdriver import ActionChains as Action_Chains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as Expected_Conditions
from selenium.webdriver.support.ui import WebDriverWait

from time import sleep

def __init__(self, webdriver, TIMEOUT=120):

# An implementation of the Wait interface that may have its timeout and polling interval configured on the fly. Configure the wait to ignore specific types of exceptions whilst waiting.

self.wait = WebDriverWait(self.webdriver, self.TIMEOUT, poll_frequency=2, ignored_exceptions=[InvalidElementStateException, StaleElementReferenceException])

def wait_for_page_to_load(self, find_element_by, element_value, expected_element_text=None, polling_duration=0):
"""
# Helper method to wait for page load. Target to find the element which renders last on the page.
#
# - find_element_by: strategies to use with "locate_value" to locate value of web element.
# - element_value: value of the web element that is looked by the strategy defined in "find_element_by".
# - expected_element_text: (Default: None) will compare the element text from the UI if (any) text given else just find that element on the page.
# - polling_duration:  Sleep interval before verify page is in ready state. (Default) is 0.
# - return: web element which responsible for page load in case there is no spinner on the page.
 """
self.wait_for_page_in_ready_state(polling_duration)
presence_of_element_located = self.webdriver.find_element(by=find_element_by, value=element_value)
Action_Chains(self.webdriver).move_to_element(presence_of_element_located).perform()
        if not expected_element_text is None:
            if expected_element_text in presence_of_element_located.text:
                return presence_of_element_located
        else:
            return presence_of_element_located
           
def wait_for_page_in_ready_state(self, polling_duration=0):
"""
# Helper method to wait for page is in ready state. Ready state here verify there is no load spinner is 
# present on the page. This modular method can be use in any point of time.
# - polling_duration:  Sleep interval before verify page is in ready state. In some scenarios page load starts after some duration(seconds) delay. (Default) is 0, in such scenarios need to pass custom polling duration. 
"""
sleep(polling_duration)
self.wait.until(Expected_Conditions.invisibility_of_element_located((By.CSS_SELECTOR, Ele)))     self.wait.until(Expected_Conditions.invisibility_of_element_located((By.CSS_SELECTOR,Ele )))     self.wait.until(Expected_Conditions.invisibility_of_element_located((By.CSS_SELECTOR, Ele)))
        

No comments:

Post a Comment