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