1、安装selenium
1 C:\Users\fnngj>python3 -m pip install selenium
直接下载selenium包: https://pypi.python.org/pypi/selenium
解压,cmd进入目录:
1 C:\selenium\selenium2.53.5> python3 setup.py install
2、安装Chrome driver
3、编写一个简单的自动化测试程序 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport unittestclass NewVisitorTest (unittest.TestCase) : """ 自动化测试是否登录百度 """ def setUp (self) : self.timeout = 40 self.browser = webdriver.Chrome() self.browser.set_page_load_timeout(self.timeout) self.wait = WebDriverWait(self.browser, self.timeout) def tearDown (self) : self.browser.quit() def test_can_start_a_list_and_retrieve_it_later (self) : self.browser.get('https://www.baidu.com' ) self.assertIn('百度' , self.browser.title) login_link = self.wait.until( EC.element_to_be_clickable((By.LINK_TEXT, '登录' ))) login_link.click() login_link_2 = self.wait.until( EC.element_to_be_clickable((By.ID, 'TANGRAM__PSP_10__footerULoginBtn' ))) login_link_2.click() username_input = self.wait.until( EC.presence_of_element_located((By.ID, 'TANGRAM__PSP_10__userName' ))) username_input.clear() username_input.send_keys('username' ) password_input = self.wait.until( EC.presence_of_element_located((By.ID, 'TANGRAM__PSP_10__password' ))) password_input.clear() password_input.send_keys('password' ) login_submit_button = self.wait.until( EC.element_to_be_clickable((By.ID, 'TANGRAM__PSP_10__submit' ))) login_submit_button.click() username_span = self.wait.until( EC.presence_of_element_located((By.CSS_SELECTOR, '#s_username_top > span' ))) self.assertEqual(username_span.text, 'username' ) if __name__ == '__main__' : unittest.main(warnings='ignore' )
评论系统未开启,无法评论!