操作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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM