Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Last active July 22, 2023 05:42
Show Gist options
  • Save Cdaprod/719731f48e7934e61de50201f1bedafe to your computer and use it in GitHub Desktop.
Save Cdaprod/719731f48e7934e61de50201f1bedafe to your computer and use it in GitHub Desktop.
Automating account registration using selenium, run as a Flask API or as a command line script
#!/usr/bin/env python3
# /registration_automation_api_updated.py
from flask import Flask, request
from flask_restful import Api, Resource
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
app = Flask(__name__)
api = Api(app)
def check_element_exists(driver, by, value):
try:
driver.find_element(by, value)
return True
except NoSuchElementException:
return False
def register_account(username, email, password):
driver = webdriver.Chrome(executable_path='path_to_chromedriver')
driver.get('https://example.com/register')
if check_element_exists(driver, By.XPATH, '//button[text()="Continue with Google"]'):
# Handle Google authentication
# ... code here ...
pass
else:
# Manual registration
username_field = driver.find_element(By.ID, 'username')
email_field = driver.find_element(By.ID, 'email')
password_field = driver.find_element(By.ID, 'password')
confirm_password_field = driver.find_element(By.ID, 'confirm_password')
username_field.send_keys(username)
email_field.send_keys(email)
password_field.send_keys(password)
confirm_password_field.send_keys(password)
register_button = driver.find_element(By.XPATH, '//button[text()="Register"]')
register_button.click()
driver.close()
class Register(Resource):
def post(self):
# Extract username, email, and password from the POST data
data = request.get_json()
username = data['username']
email = data['email']
password = data['password']
register_account(username, email, password)
return {'message': 'Registration attempted.'}
api.add_resource(Register, '/register')
if __name__ == '__main__':
choice = input("Run as (1) API or (2) Script: ")
if choice == "1":
app.run(debug=True) # Start Flask app
elif choice == "2":
# Run as script and prompt user for input
username = input("Enter username: ")
email = input("Enter email: ")
password = input("Enter password: ")
register_account(username, email, password)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment