python:selenium测试登录在chrome中闪退

博客 分享
0 199
优雅殿下
优雅殿下 2022-06-12 19:00:25
悬赏:0 积分 收藏

python:selenium测试登录在chrome中闪退

问题描述:使用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 Chromefrom chaojiying import Chaojiying_Clientfrom selenium.webdriver.common.by import Byimport timeweb = 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 webdriverfrom selenium.webdriver.firefox.service import Serviceservice = 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 timefrom selenium import webdriverfrom selenium.webdriver.chrome.service import Serviceservice = 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 Firefoxfrom selenium.webdriver.common.by import Byfrom chaojiying import Chaojiying_Clientimport timeweb = 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_pngchaojiying = 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()

 

posted @ 2022-06-12 18:27 我爱睡莲 阅读(6) 评论(0) 编辑 收藏 举报
回帖
    优雅殿下

    优雅殿下 (王者 段位)

    2018 积分 (2)粉丝 (47)源码

    小小码农,大大世界

     

    温馨提示

    亦奇源码

    最新会员