-1

I am trying to write a function that will scroll the page down to a specific element on the page. But it is not working. I am using Python + Selenium + Gherkin

My BasePage.py file

def scroll_page(self, by_locator):
    element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator))
    self.driver.execute_script("arguments[0].scrollIntoView();", element)

My HomePage.py file

from pages.BasePage import BasePage
from selenium.webdriver.common.by import By


class HomePage(BasePage):
    ManageTipsPreferencesButton = (By.XPATH, "//*[@id='root']/main/div[2]/div[2]/ul/li[1]/a[2]")

    def __init__(self, driver):
        super().__init__(driver)

    def scrollPage(self):
        self.scroll_page(self.ManageTipsPreferencesButton)

My steps.py file

@step("Scroll page to the bottom")
def step_impl(context):
    try:
        context.home_page.scrollPage()
    except:
        context.driver.close()
        assert False, "Exception! Can not scroll page"

Always assert Fales "Exception! Can not scroll page"

I do knot now where is bug. Many thanks for help

Rob
  • 14,746
  • 28
  • 47
  • 65
Michał_M
  • 11
  • 4
  • Sometimes scrolling doesn't work depending upon the web application framework you are using. It is a better option to hover over that particular element – HamzaFarooq Oct 20 '21 at 07:13

1 Answers1

0

See which one works for you

self.driver.execute_script("window.scrollTo(0, Y)")

or

self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
HamzaFarooq
  • 429
  • 1
  • 4
  • 16