Python3 Selenium自动化web测试 ==> 第六节 WebDriver高级应用 -- 操作web页面的滚动条...

本文通过一个具体的Python unittest测试案例介绍了如何使用Selenium WebDriver进行页面滚动条的操作,包括滚动到页面底部、使指定元素可见及按像素滚动等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习目的:


 

  掌握页面元素定位以外的其他重要知识点。

 

正式步骤:


 

测试Python3代码

# -*-  coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import WebDriverException
import unittest
import time
import traceback


class WebdriverAPI(unittest.TestCase):
    def setUp(self):
        # 每个用例都执行,在单个用例运行前执行
        #打开浏览器
        self.driver = webdriver.Chrome()

    def tearDown(self):
        #每个用例都执行,在单个用例运行后执行
        #退出浏览器
        self.driver.quit()

    def test_scrollBar(self):
        url= "https://www.cnblogs.com/"
        try:
            self.driver.get(url)
            self.driver.fullscreen_window()

            #使用JS的scrollTo函数和document.body.scrollHeight参数,将页面的滚动条拉到页面最下方
            self.driver.execute_script("window.scrollTo(100,document.body.scrollHeight)")
            time.sleep(3)

            #scrollIntoView(true)函数是将被遮挡的元素滚动到可见屏幕上,true表示元素显示到页面中间,
            #scrollIntoView(false是将元素滚动到屏幕底部)
            self.driver.execute_script("document.getElementById('main').scrollIntoView(true)")
            time.sleep(3)

            #使用JS的scrollBy方法,使用0和400横纵坐标参数,将页面纵向向下滚动400
            self.driver.execute_script("window.scrollBy(0,400)")
            time.sleep(3)

        except Exception:
            print(traceback.print_exc())

if __name__ == '__main__':
    unittest.main()

 

学习总结:


 

  注意单词的拼写吧

转载于:https://www.cnblogs.com/wuzhiming/p/9114303.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值