2

Trying to scroll down to the bottom of the page with selenium-webdriver python so that more products load.

driver = webdriver.Firefox()
driver.get('https://www.woolworths.com.au/Shop/Browse/back-to-school/free-school-labels')
driver.implicitly_wait(100)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(100)
driver.quit()

The webpage loads but doesn't change.

Am I missing something?

3 Answers 3

2

You can try these move_up and move_down function:

driver.maximize_window()
driver.get('https://www.woolworths.com.au/Shop/Browse/back-to-school/free-school-labels')
driver.implicitly_wait(15)
centerPanel = driver.find_element_by_css_selector("#center-panel > div[class*='hideScroll-wrapper']")
jsScript = """
        function move_up(element) {
            element.scrollTop = element.scrollTop - 1000;
        }

        function move_down(element) {
            element.scrollTop = element.scrollTop + 1000;
        }

        move_down(arguments[0]);
        move_down(arguments[0]);
        """
driver.execute_script(jsScript, centerPanel)

time.sleep(3) 

jsScript = """
        function move_up(element) {
            element.scrollTop = element.scrollTop - 1000;
        }

        function move_down(element) {
            console.log('Position before: ' + element.scrollTop);
            element.scrollTop = element.scrollTop + 1000;
            console.log('Position after: ' + element.scrollTop);
        }

        move_up(arguments[0]);
        """
driver.execute_script(jsScript, centerPanel)
Sign up to request clarification or add additional context in comments.

Comments

2

You can try using Action Chains

element = driver.find_element_by_id("id") # the element you want to scroll to 
ActionChains(driver).move_to_element(element).perform()

Comments

2

I just tried with this approach and it worked for me:

element = driver.find_element_by_xpath("//div[@class='center-content']")
driver.execute_script("return arguments[0].scrollIntoView(0, document.documentElement.scrollHeight-10);", element)

First you select the div element of the page which you want to scroll down, and then you scroll down within that element.

OBS: I added an offset when defining the scrollHeight because if you scroll to the absolute bottom it doesn't load more objects. It starts loading the catalog when you move closer to the bottom without reach it.

document.documentElement.scrollHeight-10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.