How does selenium in python automatically scroll down the page and specify the maximum sliding distance
This article is about how selenium in python can automatically scroll down the page and specify the maximum sliding distance. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The chrome that needs to be controlled by selenium slides down and loads some content automatically. The core code is:
Browser.execute_script ("window.scrollBy (0300)")
This line can slide down 300 pixels.
The required tool functions are as follows:
Def roll_window_to_bottom (browser, stop_length=None, step_length=500): "selenium scrolls the current page Slide down: param browser: selenium webdriver: param stop_length: maximum slide: param step_length: value per slide "" original_top = 0 while True: # cycle down if stop_length: if stop_length-step_length < 0: browser.execute_script ("window.scrollBy (0)" {}) ".format (stop_length) break stop_length-= step_length browser.execute_script (" window.scrollBy (0, {}) ".format (step_length)) time.sleep (0.5 + random.random ()) # pause check_height = browser.execute_script (" return document.documentElement.scrollTop | | window.pageYOffset | | document.body.scrollTop) ") if check_height = = original_top: # judge the distance from the top after sliding and the distance from the top before sliding break original_top = check_height
Examples of use:
From selenium import webdriverimport timeimport randomdef roll_window_to_bottom (browser, stop_length=None, step_length=500): "selenium scrolls the current page Slide down: param browser: selenium webdriver: param stop_length: maximum slide: param step_length: value per slide "" original_top = 0 while True: # cycle down if stop_length: if stop_length-step_length < 0: browser.execute_script ("window.scrollBy (0)" {}) ".format (stop_length) break stop_length-= step_length browser.execute_script (" window.scrollBy (0, {}) ".format (step_length)) time.sleep (0.5 + random.random ()) # pause check_height = browser.execute_script (" return document.documentElement.scrollTop | | window.pageYOffset | | document.body.scrollTop) ") if check_height = = original_top: # judge the distance from the top after sliding and the distance from the top before sliding break original_top = check_heightdef main (): option = webdriver.ChromeOptions () option.add_argument ('lang=zh_CN.UTF-8') # set browser = webdriver.Chrome (chrome_options=option) Desired_capabilities= {"page_load_strategy": "none"}) browser.get ("http://news.baidu.com/") roll_window_to_bottom (browser, stop_length=700) if _ _ name__ ='_ _ main__': main () Thank you for reading! This is the end of the article on "how to automatically scroll down the page and specify the maximum sliding distance in selenium in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!