python+selenium 登陸xx網站

1.操做系統是MAC 64bit 自帶的python,編碼是UTF-8css

2.在頁面https://pypi.python.org/pypi/selenium 上下載seleniumpython

3.將selenium的壓縮包拷貝到python目錄下,而後解壓web

4.更換管理員權限瀏覽器

5.到selenium所在的目錄下面app

6.運行命令python setup.py installui

7.在火狐瀏覽器下,錄製登陸編碼

8.selenium IDE的option下選擇General 下,勾選 "Enable experiment features"url

9.選擇format 下選擇python2 /unittest /webdriver,代碼轉換成python語言,命名爲login.pyspa

10.在腳本語言的第一行加上 #utf-8操作系統

運行python login.py

結果會出現剛剛進行的登陸操做


#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException import unittest, time, re class Login(unittest.TestCase):    def setUp(self):        self.driver = webdriver.Firefox()        self.driver.implicitly_wait(30)        self.base_url = "http://www.xxxxx.com/"        self.verificationErrors = []        self.accept_next_alert = True        def test_login(self):        driver = self.driver        driver.get(self.base_url )        for i in range(60):            try:                if u"xxxxxxxxxxxxxxxx" == driver.title: break            except: pass            time.sleep(1)        else: self.fail("time out")        try: self.assertEqual(u"xxxxxxxxxx", driver.title)        except AssertionError as e: self.verificationErrors.append(str(e))        try: self.assertEqual(u"登陸", driver.find_element_by_link_text(u"登陸").text)        except AssertionError as e: self.verificationErrors.append(str(e))        driver.find_element_by_link_text(u"登陸").click()        for i in range(60):            try:                if u"登陸 - xx" == driver.title: break            except: pass            time.sleep(1)        else: self.fail("time out")        driver.find_element_by_id("mail").clear()        driver.find_element_by_id("mail").send_keys("xxxxxx")        driver.find_element_by_id("pwd").clear()        driver.find_element_by_id("pwd").send_keys("xxxxxxx")        driver.find_element_by_css_selector("button.btn-grey.btn-regular").click()        for i in range(60):            try:                if u"xxxxxxx" == driver.title: break            except: pass            time.sleep(1)        else: self.fail("time out")        for i in range(60):            try:                if u"xxxxxxx" == driver.title: break            except: pass            time.sleep(1)        else: self.fail("time out")        try: self.assertEqual(u"退出", driver.find_element_by_link_text(u"退出").text)        except AssertionError as e: self.verificationErrors.append(str(e))        driver.find_element_by_link_text(u"退出").click()        for i in range(60):            try:                if u"首頁" == driver.find_element_by_link_text(u"首頁").text: break            except: pass            time.sleep(1)        else: self.fail("time out")        def is_element_present(self, how, what):        try: self.driver.find_element(by=how, value=what)        except NoSuchElementException, e: return False        return True        def is_alert_present(self):        try: self.driver.switch_to_alert()        except NoAlertPresentException, e: return False        return True        def close_alert_and_get_its_text(self):        try:            alert = self.driver.switch_to_alert()            alert_text = alert.text            if self.accept_next_alert:                alert.accept()            else:                alert.dismiss()            return alert_text        finally: self.accept_next_alert = True        def tearDown(self):        self.driver.quit()        self.assertEqual([], self.verificationErrors) if __name__ == "__main__":    unittest.main()

相關文章
相關標籤/搜索