Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does selenium in python automatically scroll down the page and specify the maximum sliding distance

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report