Search code examples
pythonhtmlselenium-webdriverscrollseleniumbase

How to scroll in a nav element using seleniumbase in python


<nav class="flex h-full w-full flex-col p-2 gizmo:px-3 gizmo:pb-3.5 gizmo:pt-0" aria-label="Menu">

This is the nav although, its a-lot longer its full of divs

I just want to know how to scroll till the end of the menu. Edit: loading element that has to be accounted for

<svg stroke="currentColor" fill="none" stroke-width="2" viewBox="0 0 24 24" stroke-linecap="round" stroke-linejoin="round" class="animate-spin text-center" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg">

and under it there are many line elements

top of nav xpath:

/html/body/div[1]/div[1]/div[1]/div/div/div/div/nav

top of svg xpath:

/html/body/div[1]/div[1]/div[1]/div/div/div/div/nav/div[2]/div[2]/div[2]/svg

do you need scrollbar html?


Solution

  • There are lots of specialized scroll methods in SeleniumBase:

    self.scroll_to(selector)
    self.slow_scroll_to(selector)
    self.scroll_into_view(selector)
    self.scroll_to_top()
    self.scroll_to_bottom()
    

    But you might not even need to scroll to the element. There's a self.js_click(selector) method, which lets you click on hidden elements that exist in the HTML.

    Here's an example test that uses that method to click a hidden logout menu item from the nav:

    from seleniumbase import BaseCase
    BaseCase.main(__name__, __file__)
    
    class SwagLabsLoginTests(BaseCase):
        def login_to_swag_labs(self):
            self.open("https://www.saucedemo.com")
            self.wait_for_element("div.login_logo")
            self.type("#user-name", "standard_user")
            self.type("#password", "secret_sauce")
            self.click('input[type="submit"]')
    
        def test_swag_labs_login(self):
            self.login_to_swag_labs()
            self.assert_element("div.inventory_list")
            self.assert_element('.inventory_item:contains("Backpack")')
            self.js_click("a#logout_sidebar_link")
            self.assert_element("div#login_button_container")