1. 程式人生 > >數據驅動方式登錄126郵箱

數據驅動方式登錄126郵箱

包含 encoding rip imp 密碼輸入框 頁面 find ear tab

數據文件:包含個用戶及密碼
xxxxxx,yyyyyy
xxxxx,yyyyy

#encoding=utf-8
from selenium import webdriver
import time

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

with open("userinfo.txt",encoding="utf-8") as file_obj:

    for line in file_obj:
        user = line.strip().split(",")[0]
        passwd = line.strip().split(",")[1]
        url = "https://mail.126.com"

        #打開瀏覽器
        driver = webdriver.Chrome(executable_path = "e:\\chromedriver")

        #訪問163郵箱
        driver.get(url)

        #暫停3秒,等待頁面出現,這個地方要加sleep,不然切入frame會出錯
        time.sleep(3)

        #定位登錄所在的frame
        frame = driver.find_element_by_id("x-URS-iframe")

        #切換進入frame
        driver.switch_to.frame(frame)

        time.sleep(2)

        #獲取用戶名、密碼輸入框元素對象

        username = driver.find_element_by_xpath(‘//input[@name="email"]‘)
        password = driver.find_element_by_xpath(‘//input[@name="password"]‘)

        #顯式等待用戶名輸入框出現,清空,並輸入用戶名
        WebDriverWait(driver,10).until(EC.visibility_of(username))
        username.clear()
        username.send_keys(user)

        #顯式等待密碼輸入框出現,清空,並輸入密碼
        WebDriverWait(driver,10).until(EC.visibility_of(password))
        password.clear()
        password.send_keys(passwd)

        #顯式等待定位到登錄按鈕並點擊
        WebDriverWait(driver,10).until(lambda x:x.find_element_by_id("dologin")).click()
        time.sleep(5)
        assert  "退出" in driver.page_source 

        time.sleep(5)

        driver.quit()

數據驅動方式登錄126郵箱