[Original] use Python + Selenium automatic landing sign

Tool is an extension of the human hand, with the good, and it can become part of our body, with the bad, it is just a part of someone else's body.

It is well known the truth, but the implementation of specific practice everyone is vastly different outside do not.

For example, we are familiar with web automated testing tool Selenium, a lot of people have heard about, but a lot of people are not used in the actual web test.

Before I did explain Selenium environment to build in "taught you how to build Selenuim automation environment", today I use a simple example to illustrate its specific application.

Very simple example, I use Selenium to achieve a simulated landing and sign 51testing forum.

Without further ado, directly on the code:

# -*- coding: utf-8 -*-

"""
工具说明:
使用 selenium 实现自动登陆 51testing 论坛并签到
"""

import time
from selenium import webdriver


def denglu_luntan(driver, user, pwd):
    """登录论坛

    利用 WebDriver 找到登陆窗口模拟登陆操作
    
    Args:
        driver:初始化的 webdriver
        user:用户名
        pwd:登陆密码
    
    Returns:
        无,仅单独抽取执行登陆操作
    """
    user_box = driver.find_element_by_name('username')
    user_box.send_keys(user)
    passwd_box = driver.find_element_by_name('password')
    passwd_box.send_keys(pwd)
    load_box = driver.find_element_by_name('loginsubmit')
    load_box.click()


def do_sign():
    """执行签到操作

    利用 WebDriver 找到签到的表情和输入框,并执行签到操作
    
    Args:
        无

    Returns:
        无,执行签到操作后正常退出
    """
    driver = webdriver.Chrome()
    driver.get('http://bbs.51testing.com/dsu_paulsign-sign.html')
    time.sleep(5)
    denglu_luntan(driver, 'sylan215', 'password')
    time.sleep(20)
    xiqing_png = driver.find_element_by_id('fd')
    xiqing_png.click()
    duanyu_input = driver.find_element_by_id('todaysay')
    duanyu_input.send_keys('不要怂,就是干')
    btn_png = driver.find_element_by_xpath(
        '//*[@id="qiandao"]/table[1]/tbody/tr/td/div/a/img')
    btn_png.click()
    time.sleep(30)
    driver.quit()


if __name__ == '__main__':
    do_sign()

Now I make a simple explanation for the code:

  1. denglu_luntan function is the use of simulation WebDriver forum for landing operations;
  2. do_sign WebDriver simulation function using real check operation;

This involves knowledge of specific points:

  1. Find elements: finding elements I used a total of three ways, namely find_element_by_name, find_element_by_id, find_element_by_xpath, where exactly what way, according to the actual situation to decide, in principle, to ensure uniqueness and stability;
  2. Input data: send_keys direct call function to achieve;
  3. Click operation: click directly call function to achieve;

Did not see, it is not very simple, and quickly hands it to use them.

Above, I landed 51testing forum for automatic check-ins by Python + Selenium simulation to illustrate the simple practice of Selenium.

Selenium is introduced before most places in order to better automation, automation is a reference to automation framework, in fact, these are tools, not only can we use it in test automation process, in all the places you can use tools can use it, and when we use local tools and more, you will naturally make the system tools, and will have a framework for a gradual, so do not go talking up a special tall on those things on hand from the little improvements start with, the use of any tools can be regarded as a part of automation.

I do not know whether you think so, welcome to leave a message about your views.

Of course, if you accept my point of view, + please help forward the point of a "look" to allow more people to see, thank you.

This article first appeared in public No. "sylan215", ten years older drivers test original dry goods concern me, posture up together!

sylan215

Published 110 original articles · won praise 53 · views 70000 +

Guess you like

Origin blog.csdn.net/sylan15/article/details/93112014