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

2024-10-03 02:39:38 发布

您现在位置:Python中文网/ 问答频道 /正文

当画布高度改变时,我试图截取网页的截图。其默认高度为557。我希望selenium等待高度更改为557以外的任何其他值。有办法吗

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

我尝试了EC.visibility\u的元素定位,但没能捕捉到它

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)     

Tags: nameidby高度tagdriverbodyelement
1条回答
网友
1楼 · 发布于 2024-10-03 02:39:38

定义您的等待类:

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

并称之为:

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

无论iframe长度是否从59更改,这都将打印true或false。如果不等到10秒,它就会改变,否则超时

相关问题 更多 >