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

时间:2023-03-08 22:10:05

学习目的:


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

正式步骤:


测试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()

学习总结:


  注意单词的拼写吧