from seleniumimport webdriver
from selenium.common.exceptionsimport WebDriverException
import unittest
import traceback
import time
class TestDemo(unittest.TestCase):
? ? def setUp(self):
? ? ? ? #啟動瀏覽器
? ? ? ? self.driver= webdriver.Chrome(executable_path="C:\Python\Python37\chromedriver.exe")
def test_scroll(self):
? ? ? ? url= "https://www.baidu.com/s?word=2019-03-10%0D%0A%20%E5%8F%91&tn=41052153_3_dg"
? ? ? ? #訪問selenium官網(wǎng)首頁
? ? ? ? try:
? ? ? ? ? ? self.driver.get(url)
#使用js的scrollTo函數(shù)和document.body.scrollHeight參數(shù)
? ? ? ? ? ? #將頁面滾動條滑動到頁面的最下方
? ? ? ? ? ? self.driver.execute_script("window.scrollTo(100, document.body.scrollHeight);")
#停頓3秒 用于人工驗證滾動條是否滑動到指定的位置
? ? ? ? ? ? #根據(jù)測試需要 可注釋下面的停頓代碼
? ? ? ? ? ? time.sleep(3)
#使用js的scrollIntoView函數(shù)將被遮擋的元素滾動到可見屏幕上
? ? ? ? ? ? #停頓3秒 用于人工驗滾動條是否滑動到指定的位置
? ? ? ? ? ? #根據(jù)測試需要,可注釋下面的停頓代碼
? ? ? ? ? ? # time.sleep(3)
? ? ? ? ? ? #使用js的scrollBy方法,使用0和400橫縱坐標(biāo)參數(shù)
? ? ? ? ? ? #將頁面縱向滾動400像素
? ? ? ? ? ? self.driver.execute_script("window.scrollBy(0,-400);")
#根據(jù)測試需要 可注釋下面的停頓代碼
? ? ? ? ? ? time.sleep(3)
except Exception as e:
? ? ? ? ? ? #打印異常堆棧信息
? ? ? ? ? ? print(traceback.print_exc())
def tearDown(self):
? ? ? ? ? ? #退出Chrome瀏覽器
? ? ? ? ? ? self.driver.quit()
if __name__== '__main__':
? ? unittest.main()