meta-it / odooselenium

Tools to interact with Odoo using Selenium
Other
25 stars 16 forks source link

Utility to scroll in modals #3

Open benoitbryon opened 8 years ago

benoitbryon commented 8 years ago

Sometimes, content displayed in modals is too large and too long. As a consequence, some elements (typically: caret to toggle advanced search, on the right side of the search field) cannot be clicked by Selenium. Selenium finds such elements but is_displayed() returns False.

A trick to bypass this issue is to scroll left or right until the element is visible. A shortcut to achieve this operation would be useful.

benoitbryon commented 8 years ago

Here is a snippet that uses JQuery to scroll to an element contained in last modal (not sure "last" is always "the one which is on foreground").

def scroll_modal(selector, driver, step=50):
    offset = driver.execute_script(
        "return $('.modal-dialog').last().find('.modal-body')"
        ".find('{selector}')"
        ".offset();"
        .format(selector=selector)
    )
    horizontal = 1 if float(offset[u'left']) > 0 else -1

    def scroll(driver):
        driver.execute_script(
            "$('.modal-dialog')"
            ".last()"
            ".find('.modal-body')"
            ".scrollLeft({step});"
            .format(step=horizontal * step)
        )
        modals = driver.find_elements(
            By.CSS_SELECTOR,
            '.modal-dialog'
        )
        element = modals[-1].find_element(
            By.CSS_SELECTOR,
            '.modal-body {selector}'.format(selector=selector)
        )
        if element.is_displayed():
            return element
        return False
    return scroll

Which can be used like this:

advanced_search_switch = ui.WebDriverWait(self.webdriver, 10).until(
    scroll_modal('.oe_searchview_unfold_drawer')
)