mail126.py腳本以下web
from selenium import webdriver from time import sleep from model1 import Login driver = webdriver.Chrome() driver.get("http://www.126.com") print("登陸前") title = driver.title print("title:" + title) #調用登陸 l = Login(driver) #郵箱用戶名需輸入你本身的真實的126郵箱用戶名,密碼也須要是真實的 l.login("郵箱用戶名","郵箱密碼") url = driver.current_url print("url:"+url) sleep(5) print("登錄後:") title = driver.title print("url:"+url) text = driver.find_element_by_id("spnUid").text print("user name:"+text) #斷言 assert text == "郵箱用戶名"
model1.py腳本以下url
class Login(): def __init__(self,driver): self.driver = driver def login(self,username,password): driver = self.driver driver.switch_to.frame("x-URS-iframe") driver.find_element_by_name("email").clear() driver.find_element_by_name("email").send_keys(username) driver.find_element_by_name("password").clear() driver.find_element_by_name("password").send_keys(password) driver.find_element_by_id("dologin").click()