Selenium: Wait for element's height to be not equal to X

Viewed 315

I am trying to take screenshot of webpage when canvas height changes. Its default height is 557. I want selenium to wait till height changes to any other value than 557. Is there any way to do that ?

<canvas id="faceCanvasPhoto" width="600" height="557" class="center-block reportCanvas">
    Your browser does not support the canvas element.
</canvas>

I tried EC.visibility_of_element_located but couldnt catch it

try:                                                  
    WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, "//*[@id='faceCanvasPhoto']"))
        )
    driver.find_element_by_tag_name('body').screenshot(facemap +'.png')
except TimeoutException:
    driver.find_element_by_tag_name('body').screenshot(facemap +'.png')
driver.implicitly_wait(1)     
1 Answers

Define your on wait class:

class element_height_changes(object):
    """An expectation for checking that an element has a particular css class.

      locator - used to find the element
      returns the WebElement once it has the particular css class
      """

    def __init__(self, locator, curnt_val):
        self.locator = locator
        self.curnt_val = curnt_val

    def __call__(self, driver):
        # Finding the referenced element
       element = driver.find_element(*self.locator)
       if element.get_attribute("height") == str(self.curnt_val):
          return False
       else:
          return True

And call it as :

print(WebDriverWait(driver, 10).until(
    element_height_changes(
        (By.XPATH, "//iframe"),59)
))

THis will print true or false , whether iframe length was changed from 59. If not wait till 10 second for it to change else time out.

Related