简体   繁体   中英

Selenium python unable to scroll down

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?

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

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM