selenium_页面自动循环滚动

  • 闲来无事,想起之前别人提的一个需求,希望我们的日报系统自动滚动起来,滚动播放的感觉;
    这个问题当时用js也做过,但是自己这方面知识有限没做成功,索性直接用selenium来了 ,做出来的效果也不错O(∩_∩)O哈!
  • 废话不多说,直接上代码:
# play.py
# coding=utf-8
from selenium import webdriver
import os,sys,time,datetime
class play:
    def __init__(self):
        ABSPATH = os.path.abspath(sys.argv[0])
        ABSPATH = os.path.dirname(ABSPATH)
        path = ABSPATH+'\\chromedriver_win32\\chromedriver'
        url = "http://test.com"
        options = webdriver.ChromeOptions()
        options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors"])
        self.driver = webdriver.Chrome(executable_path=path)
        self.driver.get(url)
        # driver.maximize_window()
        self.driver.set_window_size(1500, 1000)
        self.run = True

    def roll(self):
        while self.run:
            for i in range(0,2000,100):
                time.sleep(2)
                self.driver.execute_script("window.scrollBy(0,"+str(i)+")")
            self.driver.execute_script("document.body.scrollTop =0")
            time.sleep(2)
            now = datetime.datetime.now().strftime('%H:%M')
            settime = '17:38'
            if now == settime:
                self.run=False
            self.roll()
        return

    def quit(self):
        self.driver.quit()
pl = play()
pl.roll()
pl.quit()
  • 运行:
Python play.py
  • 说明:
  • 需要安装Python
  • 需要selenium库
  • 需要下载与本地环境匹配的chrmoedriver

@阴-2017-06-13 20:56:00

你可能感兴趣的:(selenium_页面自动循环滚动)