美文网首页UI自动化python自动化
python+js控制页面的滚动条滑动

python+js控制页面的滚动条滑动

作者: 那个村王富贵 | 来源:发表于2019-03-10 22:20 被阅读0次


    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官网首页

            try:

                self.driver.get(url)

    #使用js的scrollTo函数和document.body.scrollHeight参数

                #将页面滚动条滑动到页面的最下方

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

    #停顿3秒 用于人工验证滚动条是否滑动到指定的位置

                #根据测试需要 可注释下面的停顿代码

                time.sleep(3)

    #使用js的scrollIntoView函数将被遮挡的元素滚动到可见屏幕上

                #停顿3秒 用于人工验滚动条是否滑动到指定的位置

                #根据测试需要,可注释下面的停顿代码

                # time.sleep(3)

                #使用js的scrollBy方法,使用0和400横纵坐标参数

                #将页面纵向滚动400像素

                self.driver.execute_script("window.scrollBy(0,-400);")

    #根据测试需要 可注释下面的停顿代码

                time.sleep(3)

    except Exception as e:

                #打印异常堆栈信息

                print(traceback.print_exc())

    def tearDown(self):

                #退出Chrome浏览器

                self.driver.quit()

    if __name__== '__main__':

        unittest.main()

    相关文章

      网友评论

        本文标题:python+js控制页面的滚动条滑动

        本文链接:https://www.haomeiwen.com/subject/mofmpqtx.html