Selenium python unable to scroll down

老子叫甜甜 提交于 2020-02-24 11:49:29

问题


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?


回答1:


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)



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



回答3:


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


来源:https://stackoverflow.com/questions/35290451/selenium-python-unable-to-scroll-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!