0
votes

I am using Expected condition visibility_of_element_located(elementlocator) to check visibility in the webpage. But performance isn't good when an element is not visible in the page, please could someone help in improving the performance in the below code. The python code used is,

   def check_for_visibility_of_the_element(self,locator,Timeout):
        result=None
        webelement=None
        ignored_exceptions=('NoSuchElementException','StaleElementReferenceException')
        if int(Timeout)==int(BuiltIn().get_variable_value("${EXPLICIT_WAIT_TIMEOUT}")):
            if self.element == None: 
                self.element = WebDriverWait(self._driver, int(Timeout),ignored_exceptions=ignored_exceptions)
                webelement=self.element
            else:
                webelement=self.element
        else:
            self.element = WebDriverWait(self._driver, int(Timeout),ignored_exceptions=ignored_exceptions)
            webelement=self.element
            self.element=None
        elementlocator=self._getlocator(locator)
        try:
            webelement.until(EC.visibility_of_element_located(elementlocator))
        except:
            print('Unable to find the visibility of the element in given time') 
            return False       
        return True 

${EXPLICIT_WAIT_TIMEOUT} is a variable that has 45 seconds as its value. The Robot keyword that uses this keyword is

Check if Element not visible
    ${status}    ${value}    Run Keyword And Ignore Error    Element Should Be Visible    ${Locator}
    :FOR    ${i}    IN RANGE    1    20
    \    ${Boolean}=    check_for_visibility_of_the_element    ${Locator}    1
    \    Exit For Loop If    ${Boolean}==False

The explicit wait time given is 1 second in the keyword check_for_visibility_of_the_element. In each iteration, the Webdriver waits for 1 second to check if the element is visible or not. But in my case, it is taking 15 seconds to check if the element is NOT visible in the webpage, even though the waiting period given is 1 second. how to fix this issue?

1
This question is unclear. If you want a shorter wait timeout, just use a shorter wait timeout. You can pass any length of timeout you want to WebDriverWait.Bryan Oakley
I have modified the question... let me know if need more clarityJCDani
I would not use visibility_of_element_located on an element that is not supposed to be visible. I might use invisibility_of_element_located instead.Marcel Wilson

1 Answers

0
votes

My guess is that you have an implicit wait of 15 secs set somewhere in your code because of which your method takes 15 secs if the element is not visible. This is the reason why mixing implicit waits and explicit waits is not recommended.

To check if an element is visible on the page you may use find_elements method. This method will wait for the time specified in your implicit wait and return a list of all the elements identified by your locator. Comparing the length of this list to 0 should tell you whether the element is visible or not

def check_for_visibility_of_the_element(self, by, locator, timeout):
    driver.implicitly_wait(timeout)
    return len(driver.find_elements(By.by, locator)) > 0
    driver.implicitly_wait(0)