Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility to scroll in modals #3

Open
benoitbryon opened this issue Dec 7, 2015 · 1 comment
Open

Utility to scroll in modals #3

benoitbryon opened this issue Dec 7, 2015 · 1 comment

Comments

@benoitbryon
Copy link
Contributor

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
Copy link
Contributor Author

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')
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant