python:selenium測試登入在chrome中閃退

我愛睡蓮發表於2022-06-12

問題描述:使用selenium.webdriver時測試網頁,進行自動登入測試總是在登入成功時閃退。使用指定驅動器位置的方式chrome也會閃退

 

 

1.正常使用chrome驅動開啟一個網頁,正常訪問

from selenium.webdriver import Chrome

web = Chrome()
web.get("http://www.chaojiying.com/user/login/")

 

 

2.在使用selenium測試一個自動登入的程式,測試了很長時間,一直是閃退

chrome版本:版本 99.0.4844.51(正式版本) (64 位)

chromedriver版本:99.0.4844.51

seleniium版本:4.0+

from selenium.webdriver import Chrome
from chaojiying import Chaojiying_Client
from selenium.webdriver.common.by import By
import time

web = Chrome()
web.get("http://www.chaojiying.com/user/login/")
time.sleep(5) # Let the user actually see something!

# 處理驗證碼
img = web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/div/img').screenshot_as_png

#登入超級鷹

chaojiying = Chaojiying_Client('18312341234', '123456', '912345')   
dic = chaojiying.PostPic(img,1902)
verify_code = dic['pic_str']

# 想頁面中填入使用者名稱,密碼驗證碼
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys("18312341234") 
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys("123456")
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input').send_keys(verify_code)

#點選登入
time.sleep(2)
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input').click()

# driver.quit()

3.測試指定瀏覽器驅動位置

看網上的教程是,沒有指定chromedrive.exe的環境變數,或者chrome的核心版本跟chromedrive版本不一致,兩種方式都進行了重試,然後在重灌,仍然沒用,這裡是指定chromedrive.exe的程式碼部分

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

service = Service(r"D:\WebSpider\venv\Scripts\geckodriver.exe")
service.start()
driver = webdriver.Remote(service.service_url)
driver.get('http://www.chaojiying.com/user/login/')

官方文件中給到的介紹,指定chromedrive的路徑,但是實測通過這種方式開啟網頁還是閃退

import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
service = Service('/path/to/chromedriver')
service.start()
driver = webdriver.Remote(service.service_url)
driver.get('http://www.google.com/');
time.sleep(5) # Let the user actually see something!
driver.quit()

 

 

 

 

4.嘗試更換瀏覽器為火狐的,使用最新版的火狐瀏覽器,直接對應也是最新的火狐驅動

firefox驅動下載連結:https://gitHub.com/mozilla/geckodriver/releases

 

火狐瀏覽器版本:101.0.1 (64 位)

火狐驅動版本:0.31.0 (2022-04-11, b617178ef491)

 

嘗試使用指定火狐驅動開啟之前寫的程式,測試成功,問題還是出在了chrome瀏覽器中

from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from chaojiying import Chaojiying_Client
import time

web = Firefox()

web.get("http://www.chaojiying.com/user/login/")
# 處理驗證碼
img = web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/div/img').screenshot_as_png

chaojiying = Chaojiying_Client('18312341234', '123456', '912345')
dic = chaojiying.PostPic(img,1902)
verify_code = dic['pic_str']

# 想頁面中填入使用者名稱,密碼驗證碼
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input').send_keys("183312341234")
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input').send_keys("123456")
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input').send_keys(verify_code)

#點選登入
time.sleep(5)
web.find_element(By.XPATH,'/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input').click()

# driver.quit()

 

相關文章