详细代码如下:
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')
self.driver.maximize_window()
def test_alert(self):
"""execute_script:同步执行"""
self.driver.execute_script("alert('test')")
sleep(2)
self.driver.switch_to.alert.accept()
def test_title(self):
"""拿到百度的title"""
js='return document.title'
title=self.driver.execute_script(js)
print(title)
def test_red(self):
"""把百度搜索边框变为红色"""
js='var q=document.getElementById("kw");q.style.border="2px solid red"'
self.driver.execute_script(js)
def test_gundong(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.test_alert()
#case.test_title()
#case.test_red()
case.test_gundong()