None

生命就像一条蜿蜒的河流

导航

selenium学习笔记11-JavaScript操作滚动条

selenium执行JavaScript脚本

webdriver有两个方法来执行JavaScript,分别是:

execute_script同步执行

execute_async_script异步执行

通过JavaScript通常可以实现页面滚动

代码如下:

from selenium import webdriver
from time import sleep

class TestCase(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://www.baidu.com')

    def test1(self):
        self.driver.execute_script("alert('test')")
        sleep(2)
        self.driver.switch_to.alert.accept()

    def test2(self):
        js = 'return document.title'
        title = self.driver.execute_script(js)
        print(title)
    def test3(self):
        js = 'var q = document.getElementById("kw"); q.style.border="2px solid red"'
        self.driver.execute_script(js)

    def test4(self):
        self.driver.find_element_by_id('kw').send_keys('selenium')
        self.driver.find_element_by_id('su').click()
        sleep(2)
        js = 'window.scrollTo(0, document.body.scrollHeight)'
        self.driver.execute_script(js)
        sleep(2)


if __name__ == '__main__':
    case = TestCase()
    case.test3()

 

posted on 2021-02-26 10:29  我睡着了  阅读(83)  评论(0编辑  收藏  举报