selenium中关于js脚本的一些操作

from selenium import webdriver
from time import sleep

'''
selenium中关于js一些常用操作
'''
class TestCase(object):
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://www.baidu.com')
        self.driver.maximize_window()
    #   alert提示框
    def test1(self):
        self.driver.execute_script("alert('test')")
        sleep(2)
        self.driver.switch_to.alert.accept()
    #   获取当前页面的title
    def test2(self):
        js = 'return document.title'
        title = self.driver.execute_script(js)
        print(title)
    #   通过js修改百度输入框的样式
    def test3(self):
        js = 'var q = document.getElementById("kw");q.style.border="1px solid #FF0000"'
        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)


if __name__ == '__main__':
    case = TestCase()
    #   case.test1()
    #   case.test2()
    #   case.test3()
    case.test4()
    sleep(2)
    case.driver.quit()

 

posted @ 2020-08-27 09:04  橙子Cjw  阅读(175)  评论(0编辑  收藏  举报