selenium验证码登录

初始化selenium

import time
import ddddocr
from selenium import webdriver
from PIL import Image

Host = "https://www.jxjypt.cn"
browser = webdriver.Chrome('./utils/chromedriver.exe')

browser.get(Host + '/hnlgdx')
查找登录按钮
login_btn = browser.find_element_by_xpath('//*[@id="top"]/div/span/a')
login_btn.click()
查找用户名输入框
username = browser.find_element_by_id('txtaccount')
username.send_keys('4108************1557'
                  )

查找密码输入框

pwd = browser.find_element_by_id('txtpassword')
pwd.send_keys('271557')

while True:
    # 获取短信验证码图片链接
    code_picture = browser.find_element_by_id('code')
    time.sleep(2.5)
    browser.get_screenshot_as_file('./status/page.png')
    left = int(code_picture.location['x'])
    top = int(code_picture.location['y'])
    right = int(code_picture.location['x'] + code_picture.size['width'])
    bottom = int(code_picture.location['y'] + code_picture.size['height'])
    im = Image.open('./status/page.png')
    im = im.crop((left, top, right, bottom))
    im.save('./status/code.png')
    ocr = ddddocr.DdddOcr()
    with open(r'./status/code.png', 'rb') as f:
        img_bytes = f.read()
    res = ocr.classification(img_bytes)
    time.sleep(1)

    txt_code = browser.find_element_by_id('txtCode')
    time.sleep(1)
    txt_code.send_keys(res)
    time.sleep(1)

    # 获取登录按钮
    browser.find_element_by_id('btlogin').click()
    time.sleep(1)
    try:
        txt_content = browser.find_element_by_class_name('form-error-text').text
        time.sleep(1)
    except:
        break

获取2021学年下学期按钮

browser.find_element_by_xpath('//*[@id="myTab"]/li[4]/a').click()

courses = browser.find_elements_by_xpath('//*[@id="myTab_Content3"]/ul/li/div[2]/a[1][text()="开始学习"]')
time.sleep(1)
for course in courses:
    try:
        course.click()
        time.sleep(1)
        browser.switch_to_window(browser.window_handles[1])
        # 获取章节信息
        chapters = browser.find_elements_by_class_name('course-list-txt')
        time.sleep(1)
        # for chapter in chapters:
        #     chapter.click()
        hours = browser.find_elements_by_class_name('z-gery-icon')[1:]
        time.sleep(1)
        for hour in hours:
            hour.click()
            try:
                icon = hour.find_element_by_class_name('fa-check-circle')
            except:
                time.sleep(10)
                browser.find_element_by_class_name('zkjx').click()
                time.sleep(1)
                wenzi = str(browser.find_element_by_class_name('wenzi').text).strip()
                time.sleep(1)
                browser.find_element_by_xpath(f'//dl/dd/span[text()="{wenzi}."]').click()
                time.sleep(1)
            else:
                time.sleep(1)
                continue
        browser.close()
        browser.switch_to_window(browser.window_handles[0])
            time.sleep(10)
	except Exception as e:
    	pass
    # 获取章节信息

​ chapters = browser.find_elements_by_class_name(‘course-list-txt’)
​ time.sleep(1)
# for chapter in chapters:
# chapter.click()
​ hours = browser.find_elements_by_class_name(‘z-gery-icon’)[1:]
​ time.sleep(1)
​ for hour in hours:
​ hour.click()
​ try:
​ icon = hour.find_element_by_class_name(‘fa-check-circle’)
​ except:
​ time.sleep(10)
​ browser.find_element_by_class_name(‘zkjx’).click()
​ time.sleep(1)
​ wenzi = str(browser.find_element_by_class_name(‘wenzi’).text).strip()
​ time.sleep(1)
​ browser.find_element_by_xpath(f’//dl/dd/span[text()="{wenzi}."]’).click()
​ time.sleep(1)
​ else:
​ time.sleep(1)
​ continue
​ browser.close()
​ browser.switch_to_window(browser.window_handles[0])

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值