zoukankan      html  css  js  c++  java
  • python selenium chrome 实现自动化登录

    1、环境安装

    selenium的开发文档网址(英语好的可以直接看这个,写的很详细):http://selenium-python.readthedocs.io/

    因为实现的时候使用的是谷歌浏览器,在运行脚本时需要下载一个chromedriver.exe,下载地址为(注意根据自己的谷歌浏览器的版本来下载):http://npm.taobao.org/mirrors/chromedriver/

    将下载下来的chromedriver.exe放在chrome安装目录下。我的安装目录是(C:Users923AppDataLocalGoogleChromeApplication)

    配置环境变量PATH为chromedriver目录

    pip install selenium

    然后就可以写代码了:

    # coding: utf-8
    import time
    from selenium import webdriver

    from PIL import Image
    import pytesseract

    browser = webdriver.Chrome()
    browser.get('http://www.lxwc.com.cn')
    browser.maximize_window()


    # 根据路径找到按钮,并模拟进行点击
    browser.find_element_by_xpath('//*[@id="toptb"]/div/div[1]/div[3]/ul/li[2]/a').click()
    # 延时2秒,以便网页加载所有元素,避免之后找不到对应的元素
    time.sleep(1)

    # 根据路径找到按钮,并模拟进行点击
    browser.find_element_by_xpath('//*[@id="normallogin"]/a').click()
    # 延时2秒,以便网页加载所有元素,避免之后找不到对应的元素
    time.sleep(1)


    # 这里是找到输入框,发送要输入的用户名和密码,模拟登陆
    browser.find_element_by_xpath(
        "//input[@name='username']").send_keys("xxx")

    browser.find_element_by_xpath(
        "//input[@name='password']").send_keys("xxx")

    # 在输入用户名和密码之后,点击登陆按钮
    browser.find_element_by_xpath("//button[@name='loginsubmit']").click()

    #验证码识别,未完成

    element = browser.find_element_by_xpath("//img[@class='vm']")

    location = element.location
    size = element.size
    left = location['x']
    top =  location['y']
    right = location['x'] + size['width']
    bottom = location['y'] + size['height']

    print(location)
    print(size)

    # 打开目标网站,并截取完整的图片
    browser.get_screenshot_as_file('login.png')
    im = Image.open('login.png')
    im = im.crop((left, top, right, bottom))
    im.save('code.png')

    验证码部分:调用Pytesseract

    https://www.jianshu.com/p/afcde49c57b7

  • 相关阅读:
    class的大小
    派生表中第一个基类没有虚函数,派生类存在虚函数时的内存布局
    转: inline关键字使用
    三角形面积计算(海伦公式或叉积绝对值的一半)
    单继承时虚函数表解析
    C语言中的atan和atan2
    n皇后问题的递归和迭代版 leetcode N-Queens
    linux网络编程中INADDR_ANY的含义
    数据库有哪些设计技巧
    原来Github上也有这么多的JavaScript学习资源!
  • 原文地址:https://www.cnblogs.com/cute/p/11642926.html
Copyright © 2011-2022 走看看