Selenium:等待元素的高度不等于X

Roy*_*yal 5 python selenium

我试图在画布高度发生变化时截取网页的屏幕截图。它的默认高度是 557。我希望 selenium 等待高度更改为 557 以外的任何其他值。有什么办法可以做到这一点吗?

<canvas id="faceCanvasPhoto" width="600" height="557" class="center-block reportCanvas">
    Your browser does not support the canvas element.
</canvas>
Run Code Online (Sandbox Code Playgroud)

我尝试了 EC.visibility_of_element_ located 但无法捕获它

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)     
Run Code Online (Sandbox Code Playgroud)

PDH*_*ide 1

定义您的等待类别:

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
Run Code Online (Sandbox Code Playgroud)

并将其称为:

print(WebDriverWait(driver, 10).until(
    element_height_changes(
        (By.XPATH, "//iframe"),59)
))
Run Code Online (Sandbox Code Playgroud)

这将打印 true 或 false ,无论 iframe 长度是否从 59 更改。如果没有等到 10 秒更改,否则超时。