python 登陆脚本

最核心的关键是对比登录前后网页的变化

  1.  代码版本1
  2. # coding=utf-8#
    #导入webdriver模块
    from selenium import webdriver
    from time import sleep
    # webdriver的本地目录,指定到exe
    execute_path = r'C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe'
    equal_string='2016 © 南京老人佳智能科技有限公司'
    # 指定是chrome 的驱动
    # 执行到这里的时候Selenium会去到指定的路径将chrome driver 程序运行起来
    driver=webdriver.Chrome(execute_path)
    # 使用get 方法打开指定的网站
    driver.get('http://itest.chinacloudapp.cn:8280/')
    # 获取网页上面的元素 find_element_by_id_xxx
    #· text 获取该元素的文本

    # 模拟登陆操作
    # 获取定位,输入值

    login_url=driver.current_url
    driver.find_element_by_id('userName').send_keys('ly')
    driver.find_element_by_id('password').send_keys('ly123!@#')
    #查询包含doLogin的字符
    driver.find_element_by_css_selector('button[onclick*="doLogin"]').click()

    # 隐式等待
    sleep(2)
    # driver.implicitly_wait(10)

    # cookies=driver.get_cookie()
    #
    # print cookies

    homepage_url=driver.current_url

    if homepage_url==login_url:
    print '登陆失败'
    else:
    print '登陆成功'

    # 检查版本信息

    try:
    ele=driver.find_element_by_class_name("copyright").text
    print ele
    # 使用文字对比的时候,需要考虑编码
    if ele == equal_string.decode('utf-8'):
    print '版本信息正确'
    else:
    print '版本信息错误'
    except:
    print '版本信息错误'
    driver.close()




原文地址:https://www.cnblogs.com/gasx/p/7486929.html