日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用selenium进行模拟登录

發布時間:2024/3/13 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用selenium进行模拟登录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

模擬登陸12306
.對12306進行模擬登陸.
需使用到超級鷹. 以對超級鷹板塊截切。。。
http://www.chaojiying.com/user/mysoft/

import time from PIL import Image from base64 import b64decode from selenium.webdriver import ActionChains #對12306界面發請求 bro=webdriver.Chrome(executable_path=chromedriver路徑') bro.get('https://kyfw.12306.cn/') bro.maximize_window()#網頁最大化 time.sleep(3) #點擊到賬號登陸 #find_elements_by_xpath無法點擊 a=bro.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]/a') a.click() time.sleep(3) #對整個界面截圖 bro.save_screenshot('screen.png') #用xpath找到驗證碼位置 定位圖片位置,獲取 src 的屬性值 #get_attribute(‘src’) 會把換行符替換為"%0A",因此解碼的時候要替換回去。 code_img_ele=bro.find_element_by_xpath('//*[@id="J-loginImg"]').get_attribute('src') code_img_name= code_img_ele.split(",")[-1] # 刪除前面的 “data:image/jpeg;base64,” img_str = code_img_name.replace("%0A", '\n') # 將"%0A"替換為換行符 img_data = b64decode(img_str) # b64decode 解碼 #保存圖片 with open('./captcha.jpeg', 'wb') as fout:fout.write(img_data)fout.close() ```chaojiying = Chaojiying_Client('xxxx', 'xxxxx', '******') #用戶中心>>軟件ID 生成一個替換 96001 im = open('./captcha.jpeg', 'rb').read() #本地圖片文件路徑 來替換 a.jpg 有時WIN系統須要// print (chaojiying.PostPic(im, 9004)['pic_str']) result=chaojiying.PostPic(im, 9004)['pic_str'] time.sleep(0.5) all_list=[]#存儲坐標 #定位到驗證碼的區域 if '|' in result:list_1 = result.split('|')#將超級鷹返回的坐標用‘|’分割列表count_1=len(list_1)#list_1的長度for i in range(count_1):xy_list=[]x=int(list_1[i].split(',')[0])y = int(list_1[i].split(',')[1])xy_list.append(x)xy_list.append(y)all_list.append(xy_list) else:xy_list = []x = int(result.split(',')[0])y = int(result.split(',')[1])xy_list.append(x)xy_list.append(y)all_list.append(xy_list) for loc in all_list:x=loc[0]y=loc[1]#實例化動作鏈,并且定位到驗證碼圖片里進行點擊正確的圖片code_img_eles = bro.find_element_by_xpath('//*[@id="J-loginImg"]')ActionChains(bro).move_to_element_with_offset(code_img_eles, x, y).click().perform()time.sleep(0.5) #輸入賬號與密碼 bro.find_element_by_id('J-userName').send_keys('*******') time.sleep(1) bro.find_element_by_id('J-password').send_keys('*******') time.sleep(1) bro.find_element_by_id('J-login').click() time.sleep(1) #出現滑塊,并對滑塊進行處理 span=bro.find_element_by_id('nc_1_n1z') action=ActionChains(bro) action.click_and_hold(span) action.drag_and_drop_by_offset(span,400,0).perform() while True:try:info=bro.find_element_by_xpath('//*[@id="J-slide-passcode"]/div/span').textprint(info)if info=='哎呀,出錯了,點擊刷新再來一次':#點擊刷新bro.find_element_by_xpath('//*[@id="J-slide-passcode"]/div/span/a').click()time.sleep(0.2)#重新移動滑塊span = bro.find_element_by_xpath('//*[@id="nc_1_n1z"]')action = ActionChains(bro)# 點擊長按指定的標簽action.click_and_hold(span).perform()action.drag_and_drop_by_offset(span, 400, 0).perform()time.sleep(5)except:print('ok!')break action.release() time.sleep(50) bro.quit()本文僅做供學習交流,內容僅做參考

總結

以上是生活随笔為你收集整理的使用selenium进行模拟登录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。