webdriver高级应用(2) - 滚动条操作

本文介绍使用Selenium进行网页滚动条自动化测试的方法,通过Python脚本实现页面滚动至底部、定位元素滚动以及滚动指定距离的功能,适用于Web自动化测试场景。

webdriver高级应用(2) - 滚动条操作

 

#-*- coding:utf-8 -*-
from selenium import webdriver
import unittest
import traceback
import time

class TestDemo(unittest.TestCase):

    def setUp(self):
        #启动chrome浏览器
        self.driver = webdriver.Chrome(executable_path="C:\geckodriver")

    def test_scroll(self):
        url = "https://www.seleniumhq.org/"
        #访问selenium官网首页
        try:
            self.driver.get(url)

            self.driver.execute_script\
                ("window.scrollTo(100,document.body.scrollHeight);")


            time.sleep(3)

            #scrollIntoView(true)
            #scrollIntoView(false)

            self.driver.execute_script\
                ("document.getElementById('choice').scrollIntoView(true);")

            time.sleep(3)


            self.driver.execute_script("window.scrollBy(0,400);")
            time.sleep(3)

        except Exception, e:

            print(traceback.print_exc())

    def tearDown(self):
        self.quit()
if __name__ == "main":
    unittest.main()

  

转载于:https://www.cnblogs.com/aszeno/p/10335588.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值