ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-06-12 18:34:58  阅读:209  来源: 互联网

标签:web 中闪退 webdriver python selenium import div find


问题描述:使用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()

 

标签:web,中闪退,webdriver,python,selenium,import,div,find
来源: https://www.cnblogs.com/houzhiheng/p/16368542.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有