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
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页