tampermonkey油猴与selenium webdriver入门 (by quqi99)

作者:张华 发表于:2020-11-08
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明

今天群里在给同学投票,有人点50次,于是,研究了一下。

tampermonkey油猴

下面脚本运行不成功, 因为还得处理跨域问题,不怎么熟悉javascript也就没继续研究了,记录一下只是为了说明油猴的入门及调试方法。
另外,在调试的过程中,遇到油猴脚本总不生效,后来在卸载安装最新版本就好了。

// ==UserScript==
// @name         test
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://weiweiyouxiang.com:81/index.php?app_act=detail&id=*
// @require      https://code.jquery.com/jquery-3.3.1.min.js
// @grant        GM_xmlhttpRequest
// @grant        GM_download
// @connect      s95.cnzz.com
// ==/UserScript==

let clickCount = 0;
let totalCount = 100;

(function() {
    'use strict';

//    var script = document.createElement('script');
//    script.type = 'text/javascript';
//    script.src = document.location.protocol + '//s95.cnzz.com/stat.php?id=xxx';
//    document.getElementsByTagName('head')[0].appendChild(script)
    GM_xmlhttpRequest({
    method: "GET",
    url: document.location.protocol + '//s95.cnzz.com/stat.php?id=xxx',
    onload: function(res) {
        if (res.status == 200) {
            //var text = res.responseText;
            //var json = JSON.parse(text);
            //console.log(json);
        }
      }
    });
    vote();
})();

function vote () {
  clickCount = 0;
  try {
    document.execCommand('Refresh')
  }catch (e) {
    alert(e);
  }
  $(document).ready(function(){
    let $theI = $("#btntoupiao")
    doclick($theI);
  });
  // 一小时后再次投票
  setTimeout(vote, 1000 * 60 * 60);
}

function doclick ($i) {
  $i.click();
  clickCount ++ ;
  totalCount ++ ;
  console.log('投票了' + totalCount + '次');
  if (clickCount <= 1) {
    // 最多投1次 每次点击间隔10s
    setTimeout(() => {doclick($i)}, 1000 * 1);
  }
}

selenium webdriver

$ cat vote.py 
#!/usr/bin/env python
# coding=utf-8
# Usage
# /usr/bin/python3 -m pip install --upgrade pip
# pip3 install selenium
# google-chrome-stable --version  #eg: mine is 86.0.4240.183
# https://chromedriver.chromium.org/downloads
# wget https://chromedriver.storage.googleapis.com/86.0.4240.22/chromedriver_linux64.zip
# unzip chromedriver_linux64.zip -d /home/hua/drivers/
# python3 vote.py

import time
import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options

chromeOptions = Options()
#Press 'F12' in chrome to client the left place of 'Console' to open 'Network conditions' to change 'User agent'
chromeOptions.add_argument('user-agent="Mozilla/5.0 (Linux; Android 10; Pixel 4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Mobile Safari/537.36"')
chromeOptions.headless = True
browser = webdriver.Chrome(executable_path="/home/hua/drivers/chromedriver", options=chromeOptions)
browser.get("http://weiweiyouxiang.com:81/index.php?app_act=detail&id=xxx")
print("Title: %s" % browser.title)
maxNum = random.randint(1,50)
for num in range(1,maxNum):
    print("Vote number : %s" % browser.find_element_by_id("id_vote").text)
    button = browser.find_element_by_xpath('//*[@id="id_btntoupiao"]')
    browser.execute_script("arguments[0].click();", button)
    time.sleep(5)
    browser.refresh()
    print("Vote number after freshing the page : %s" % browser.find_element_by_id("id_vote").text)

Fake Display

在无GUI的ubuntu server上,需要安装一个假的显示器模拟。

apt install -y xvfb
pip3 install pyvirtualdisplay

Others - Install chrome etc

wget -N https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb -P ./
dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb
apt-get -f install -y
dpkg -i --force-depends ~/google-chrome-stable_current_amd64.deb

wget -N https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar -P ./
xvfb-run java -Dwebdriver.chrome.driver=/home/hua/drivers/chromedriver -jar /home/hua/drivers/selenium-server-standalone-3.141.59.jar
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

quqi99

你的鼓励就是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值