日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Python利用Selenium實(shí)現(xiàn)自動(dòng)化驗(yàn)證登錄

作者:幻影123! 更新時(shí)間: 2022-08-15 編程語言

Python里面使用Selenium是一個(gè)很重要的自動(dòng)化測試模塊,我們可以用它寫一個(gè)驗(yàn)證登錄腳本,下面是一個(gè)簡單的demo:

from selenium import webdriver
from selenium.webdriver.common.by import By #用于定位class元素
from selenium.webdriver.support.ui import WebDriverWait #等待
from selenium.webdriver.support import expected_conditions as EC #執(zhí)行條件
import time
#我們可以把賬號(hào)密碼放在txt文件中,用||分隔
with open(filename.txt,'r') as f:
	account=f.read().split('||')

name=account[0]
password=account[1]
#1.創(chuàng)建瀏覽器對象
options=webdriver.EdgeOptions()
options.headless=True#無頭瀏模式
#以下設(shè)置可以將模擬瀏覽器偽裝成自己常用的瀏覽器
prefs={'profile.default_content_settings_popups':0}
options.add_experimental_option('prefs',prefs)
options.add_argument(r"--user-data-dir=D:\Users\zhangsanlisi\AppData\Local\Microsoft\Edge\User Data copy") # 設(shè)置成用戶自己的數(shù)據(jù)目錄,這里有個(gè)坑,需要把自己的瀏覽器用戶目錄的東西復(fù)制一份改個(gè)名字設(shè)置到這里引用,否則會(huì)報(bào)錯(cuò)
web =webdriver.Edge(options=options)
html=web.get('https://xxxx.sod.com/sse/login')
WebDriverWait(web, 10).until(EC.presence_of_element_located((By.XPATH, "http://div//input[@id='username']")) #獲取帶有a標(biāo)簽的inntertext中的關(guān)鍵字,并給與網(wǎng)頁最大10秒的加載時(shí)間
).clear()
WebDriverWait(web, 10).until(EC.presence_of_element_located((By.XPATH, "http://div//input[@id='username']")) #獲取帶有a標(biāo)簽的inntertext中的關(guān)鍵字,并給與網(wǎng)頁最大10秒的加載時(shí)間
).send_keys(name)
WebDriverWait(web, 10).until(EC.presence_of_element_located((By.XPATH, "http://div//input[@id='password']")) #獲取帶有a標(biāo)簽的inntertext中的關(guān)鍵字,并給與網(wǎng)頁最大10秒的加載時(shí)間
).clear()
WebDriverWait(web, 10).until(EC.presence_of_element_located((By.XPATH, "http://div//input[@id='password']")) #獲取帶有a標(biāo)簽的inntertext中的關(guān)鍵字,并給與網(wǎng)頁最大10秒的加載時(shí)間
).send_keys(password)
WebDriverWait(web, 10).until(EC.presence_of_element_located((By.XPATH, "http://div//input[@id='formsubmitButton']")) #獲取帶有a標(biāo)簽的inntertext中的關(guān)鍵字,并給與網(wǎng)頁最大10秒的加載時(shí)間
).click()
time.sleep(5)
#2.打開網(wǎng)址
web.get('http://xxxx.ewdt.com/')
#把獲取到的cookie連接起來
cookie='; '.join([(i['name']+'='+i['value']) for i in  web.get_cookies()])
with open('cookie.txt','w') as f:
   f.write(cookie)
web.close()
print('cookie已更新')

原文鏈接:https://blog.csdn.net/qq_33909788/article/details/126336575

欄目分類
最近更新