Skip to content

Instantly share code, notes, and snippets.

@pikulet
Created October 22, 2021 12:11
Show Gist options
  • Save pikulet/50c6e9c44f855139397124072ab9f43b to your computer and use it in GitHub Desktop.
Save pikulet/50c6e9c44f855139397124072ab9f43b to your computer and use it in GitHub Desktop.
web clicker macro using selenium
import sys
import json
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
## set loop
i = 0
loops = 1
if len(sys.argv) > 1:
loops = sys.argv[1]
if loops == 'inf':
condition = True
else:
condition = i < loops
## get input
with open('input.json') as f:
data = json.load(f)
url = data['url']
xpath = '//*[contains(text(), "{text}")]'
paths = list(map(lambda text: xpath.format(text=text), data['paths']))
## run macro
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 100)
while condition:
driver.get(url)
try:
for elem in paths:
button = wait.until(
EC.presence_of_element_located(By.XPATH, elem))
button.click()
i += 1
except:
print('cannot find btn', elem)
break
#driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment