Skip to content

Instantly share code, notes, and snippets.

@guanhuamai
Last active March 14, 2017 15:20
Show Gist options
  • Save guanhuamai/967b5a9de94c6d3dc9553992427499af to your computer and use it in GitHub Desktop.
Save guanhuamai/967b5a9de94c6d3dc9553992427499af to your computer and use it in GitHub Desktop.
AutoClicker.py
from selenium import webdriver
from selenium.webdriver.common.action_chains import *
import time
"""
usage: hehe ni dong de
dependence: selenium(an api), chromedriver(an app)
install selenium with: pip install -i https://pypi.douban.com/simple selenium
download chromedriver.exe latest version in: http://chromedriver.storage.googleapis.com/index.html?path=2.24/
coded by mgh....
"""
class Clicker(object):
def __init__(self):
"""
: add cookie to the chrome driver
"""
self.chrome = webdriver.Chrome(executable_path=r'C:\chromedriver.exe')
self.chrome.get('http://10.214.147.101:12345')
self.chrome.add_cookie({'name': 'ppr_nickname', 'value': 'test'})
def click(self, eles_name, i, x, y):
"""
:param eles_name: names of candidate elements
:param i: index of element in eles_names
:param x: horizontal offset where the element is clicked
:param y: vertical offset where the element is clicked
:return:
click event may not work, adjust your offset please,
"""
actions = ActionChains(self.chrome)
btn = self.chrome.find_elements_by_xpath(eles_name)[i]
actions.move_to_element_with_offset(btn, x, y).click()
actions.perform()
def do_survey(self, url, num, x, y):
"""
:param url: survey page url
:param num: number of clicks in a page
:param x: horizontal offset where the element is clicked
:param y: vertical offset where the element is clicked
:return:
"""
self.chrome.get(url)
for i in range(num):
self.click("//ul[@id='sortable_right']", 0, x, y)
time.sleep(1)
self.click("//button[@id='button_workload']", 1, 5, 5)
time.sleep(1)
self.click("//button[@id='button_confirm']", 0, 5, 5)
if __name__ == "__main__":
c = Clicker()
_url = 'http://10.214.147.101:12345/part1?qid='
for _i in range(1, 21, 1):
c.do_survey(_url + str(_i), 50, 5, 5)
for _i in range(21, 41, 1):
c.do_survey(_url + str(_i), 20, 40, 40)
for _i in range(41, 61, 1):
c.do_survey(_url + str(_i), 50, 5, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment