1 from selenium import webdriver 2 from time import sleep 3 4 class testCase(object): 5 def __init__(self): 6 self.driver = webdriver.Chrome() 7 self.driver.get('http://www.baidu.com') 8 9 def execute_script(self): 10 self.driver.execute_script("alert('百度一下,你就知道')") 11 sleep(2) 12 self.driver.switch_to.alert.accept() 13 def test1(self): 14 js = 'return document.title' 15 title = self.driver.execute_script(js) 16 print(title) 17 18 def test2(self): 19 js = "var q = document.getElementById('kw'); q.style.border = '2px solid red'" 20 self.driver.execute_script(js) 21 22 def scrollTo(self): 23 self.driver.find_element_by_id('kw').send_keys('赵文卓') 24 self.driver.find_element_by_id('su').click() 25 sleep(2) 26 self.driver.execute_script('window.scrollTo(0, document.body.scrollHeight)') 27 28 sleep(2) 29 self.driver.quit() 30 if __name__ == '__main__': 31 case = testCase() 32 case.execute_script() 33 case.test1() 34 case.test2() 35 case.scrollTo()