【python】使用Splinter自动化输入文本以及点击网页按钮

同学写了个网页,页面有一个点赞的按钮,于是恶作剧写了个脚本自动化点赞,直接点到了“23333”……

工具:python+splinter

splinter安装:

sudo pip install splinter

打开python,直接命令行下输入:

from splinter import Browser

url = 'your website'
#open your browser
browser = Browser()
#visit your website
browser.visit(url)
#loop
while (1):
    #'goodBoy' is the id of the button
    browser.find_by_id('goodBoy').click()
browser.quit()

……于是它会打开你的浏览器,访问指定网站并不断地点击id对应的按钮!

以上纯属无聊玩一玩~实际上这个功能还是挺实用的!比如下面我们来模拟下登陆126邮箱吧!

#coding=utf-8
import time
from splinter import Browser

def splinter(url):
    browser = Browser()
    #login 126 email websize
    browser.visit(url)
    #wait web element loading
    time.sleep(5)
    #fill in account and password
    browser.find_by_id('idInput').fill('xxxxxx')
    browser.find_by_id('pwdInput').fill('xxxxx')
    #click the button of login
    browser.find_by_id('loginBtn').click()
    time.sleep(8)
    #close the window of brower
    browser.quit()

if __name__ == '__main__':
    websize ='http://www.126.com'
    splinter(websize)

以上代码实现了:打开126的网站,在用户名和密码栏输入账号密码,并点击登陆按钮进行登陆~

你可能感兴趣的:(python,网页按钮,自动点击)