Using selenium Webdriver, how to check if a web element is out of the visible area of ​​the screen?

I need to check that the item is not showing before scrolling the webpage. The item is actually present on the web page but not displayed on the screen. When I browse a web page, the item becomes visible to the user.

How can I automate the above scenario?

+3
source share
3 answers

I found a way to check if the item was visible.

The idea is to get a 1. scrollable page position; 2. height of the visible area of ​​the browser; and 3. y-location element.

    scroll_position_script = """
        var pageY;
        if (typeof(window.pageYOffset) == 'number') {
            pageY = window.pageYOffset;
        } else {
            pageY = document.documentElement.scrollTop;
        }
        return pageY;
    """

    yOffset = self.browser.execute_script(scroll_position_script)

    js_client_height = "return document.documentElement.clientHeight;"
    browser_height = self.browser.execute_script(js_client_height)

    elem_yloc = int(element.location['y'])
    self.assertTrue(elem_yloc>=yOffset and elem_yloc<=yOffset+browser_height)
+3
source

, .

  • , ?

  • ?

  • ?

0

@haroonzone. .

, getLocation() getSize() WebElement.

driver.manage().window(). getSize().

, , JavaScript window. innerHeight innerWidth window, document , , document.body DOM. , , clientWidth clientHeight.

This link gives some preliminary tips for IE, since IE obviously works differently.

Try it and see how it happened.

0
source

All Articles