selenium模拟登录拉勾(超级鹰)

一、打开网页

url:https://www.lagou.com/

二、关闭弹窗

 

三、点击登录

 

四、输入账号密码

 

五、勾选同意协议并登录

 六、超级鹰验证码识别

 七、判断是否登录成功与是否失败次数过多

判断是否登录成功:随机选择一个当前页面没有的元素,而成功登录页面存在的元素.

 

 源代码

 

web = Chrome()
web.get("https://www.lagou.com/")
web.find_element_by_xpath('//*[@id="changeCityBox"]/p[1]/a').click()
sleep(0.5)
# 进入登录页面
web.find_element_by_xpath('//*[@id="lg_tbar"]/div[1]/div[2]/ul/li[1]').click()
sleep(0.5)
# 切换至密码登录
web.find_element_by_class_name('change-login-type').click()
sleep(0.5)
# 输入账户密码
web.find_element_by_xpath('//div[@data-propertyname="username"]/div/input').send_keys(
            username)
web.find_element_by_xpath('//div[@data-propertyname="password"]/div/input').send_keys(
            password)
web.find_element_by_xpath('//div[@class="login-checked"]').click()
# 点击验证码
sleep(0.5)
web.find_element_by_xpath('/html/body/div[3]/div[1]/div/div/div[2]/div[3]/div[2]/div[2]/div[2]').click()
sleep(0.5)
chaojiying = Chaojiying_Client(username,password, '915491')
j = 1
while True:
# 判断是否发生异常,多次识别失败
    print(f"正在尝试第{j}次登录")
    j += 1
    try:
        web.find_element_by_xpath('/html/body//div[@class="geetest_panel_error_content"]').click()
        sleep(2)
    except:
        sleep(1)
    img = web.find_element_by_xpath(
'//div[@class="geetest_holder geetest_silver"]/div[@class="geetest_widget"]')
    # 超级鹰获取物品位置
    offset_list = chaojiying.PostPic(img.screenshot_as_png, 9004)['pic_str'].split('|')
    # 点击物品
    for i in offset_list:
        temp = i.split(',')
        x = int(temp[0])
        y = int(temp[1])
        ActionChains(web).move_to_element_with_offset(img, x, y).click().perform()
        sleep(1)
        web.find_element_by_xpath('//div[@class="geetest_panel"]/a/div').click()
        sleep(5)
        # 是否成功登录,成功登录是,is_login匹配的元素不为空
        try:
            is_login = web.find_element_by_xpath('//[@id="lg_tbar"]/div[1]/div[2]/ul/li[2]/a')
            break
        except:
            pass

 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>