百度登录、退出示例
1 # -*- coding:utf-8 -*- 2 """ 3 百度登录,退出 4 """ 5 from selenium import webdriver 6 import unittest 7 from selenium.webdriver import ActionChains 8 from selenium.webdriver.support.ui import WebDriverWait 9 10 class Baidu_Demo(unittest.TestCase): 11 12 def setUp(self): 13 self.driver = webdriver.Firefox() 14 self.driver.get('http://www.baidu.com') 15 16 def test_login(self): 17 driver = self.driver 18 driver.find_element_by_xpath("//*[@id='lb']").click() 19 driver.find_element_by_name('userName').send_keys('你的帐号') 20 driver.find_element_by_name('password').send_keys('你的密码') 21 driver.find_element_by_name('memberPass').click() #去掉记住密码 22 driver.find_element_by_id('TANGRAM__PSP_10__submit').click() 23 24 try: 25 WebDriverWait(driver, 5).until(lambda x: x.find_element_by_id('s_username_top')) 26 myele = driver.find_element_by_id('s_username_top') 27 myele_text = myele.text 28 29 action_myele = ActionChains(driver) 30 action_myele.move_to_element(myele).perform() #移动鼠标到元素我的帐号上 31 32 WebDriverWait(driver, 5).until(lambda x: x.find_element_by_link_text('退出')) 33 driver.find_element_by_link_text('退出').click() #退出登录 34 except Exception as e: 35 print e 36 37 self.assertEqual("你的帐号", myele_text, '登录失败') 38 39 def tearDown(self): 40 self.driver.quit() 41 42 if __name__ == '__main__': 43 unittest.main()
1 # -*- coding:utf-8 -*- 2 """ 3 百度登录,退出 4 """ 5 from selenium import webdriver 6 import unittest 7 from selenium.webdriver import ActionChains 8 from selenium.webdriver.support.ui import WebDriverWait 9 driver = webdriver.Firefox() 10 class Baidu_Demo(unittest.TestCase): 11 12 def test_login(self): 13 driver.get('http://www.baidu.com') 14 driver.find_element_by_xpath("//*[@id='lb']").click() 15 driver.find_element_by_name('userName').send_keys('expect88') 16 driver.find_element_by_name('password').send_keys('luoqing1227') 17 driver.find_element_by_name('memberPass').click() #去掉记住密码 18 driver.find_element_by_id('TANGRAM__PSP_10__submit').click() 19 20 self.assertTrue('expect88' in driver.page_source) 21 22 def test_qiut(self): 23 WebDriverWait(driver, 5).until(lambda x: x.find_element_by_id('s_username_top')) 24 myele = driver.find_element_by_id('s_username_top') 25 26 action_myele = ActionChains(driver) 27 action_myele.move_to_element(myele).perform() #移动鼠标到元素我的帐号上 28 29 WebDriverWait(driver, 5).until(lambda x: x.find_element_by_link_text('退出')) 30 driver.find_element_by_link_text('退出').click() 31 32 if __name__ == '__main__': 33 unittest.main()