zoukankan      html  css  js  c++  java
  • 操作web页面的滚动条

    (1)滑动页面的滚动条到页面的最下面

    (2)滑动页面的滚动条到页面的某个元素

    (3)滑动页面的滚公条向下移动某个数量的像素

    #!usr/bin/env python  
    #-*- coding:utf-8 -*-  
    """ 
    @author:   sleeping_cat
    @Contact : zwy24zwy@163.com
    """ 
    #操作web页面的滚动条
    
    from selenium import webdriver
    import unittest
    import traceback
    import time
    
    class TestDemo(unittest.TestCase):
        def setUp(self):
            self.driver = webdriver.Chrome()
    
        def test_scroll(self):
            url = 'http://111.160.124.227:9194/'
            try:
                self.driver.get(url)
                self.driver.execute_script('window.scrollTo(100,document.body.scrollHeight);')
                #使用JavaScript的scrollTo函数和document.body.scrollHeight参数
                #将页面的滚动条滑动到页面的最下方
                  time.sleep(2)
                self.driver.execute_script
                ('document.getElementById("namekeyword").scrollIntoView(true);')
                #使用JavaScript的scrollIntoView函数将被遮挡的元素滚到可见屏幕上
                  #scrollIntoView(true)将元素滚动到屏幕中间
                  #scrollIntoView(false)将元素滚动到屏幕底部
                  time.sleep(2)
                self.driver.execute_script('window.scrollBy(0,400);')
                #使用JavaScript的scrollBy方法,使用0和400横纵坐标参数
                  time.sleep(2)
            except Exception as e:
                print(traceback.print_exc())
    
        def tearDown(self):
            self.driver.quit()
    
    if __name__ == '__main__':
        unittest.main()
  • 相关阅读:
    HashMap源码分析
    LinkedList源码分析
    ArrayList源码学习
    Java容器知识总结
    Collections 工具类和 Arrays 工具类常见方法
    Java基础知识
    MySQL高级之索引优化分析
    MySQL命令大全
    Java IO
    SpringCloud笔记
  • 原文地址:https://www.cnblogs.com/sleeping-cat/p/8117827.html
Copyright © 2011-2022 走看看