Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.4 kB
2
Indexable
Never
from selenium import webdriver
import time
option = webdriver.ChromeOptions()
import json
import random
from selenium.webdriver.common.by import By
option.add_argument("-incognito")
#option.add_argument("--headless")
#option.add_argument("disable-gpu")

with open("input.json", "r") as input_file:
    input_json = json.load(input_file)

browser = webdriver.Chrome(executable_path='/Users/shachisingh/Documents/code/googleFormScript/chromedriver_mac_arm64/chromedriver', options=option)

def getRequiredNames(no_of_names):
    names = []
    with open("names") as file:
        if no_of_names > 6000:
            raise Exception("Too large number of names asked")
        
        for i in range(no_of_names):
            names.append(file.readline())
    return names


def shachi(name):

    primary_owner = "shachi"
    page_var = "firstPage"
    browser.get(input_json[primary_owner]["url"])

    browser.implicitly_wait(20)
    time.sleep(2)


    check_box = browser.find_element(By.XPATH,input_json[primary_owner][page_var]["xPathFirstTextBox"])
    check_box.click()

    
    submit_button = browser.find_element(By.XPATH,input_json[primary_owner][page_var]["xPathFirstClick"])
    submit_button.click()


    #second page
    page_var = "secondPage"
    time.sleep(2)
    browser.implicitly_wait(2)
    # print("first input box->", input_json[primary_owner][page_var]["firstName"])
    text_box_1 = browser.find_element(By.XPATH, input_json[primary_owner][page_var]["firstName"])
    text_box_1.send_keys(name)

    radio_box = browser.find_element(By.XPATH, input_json[primary_owner][page_var]["gender"][
        random.randint(0,len(input_json[primary_owner][page_var]["gender"])-1)
    ])
    radio_box.click()

    radio_box = browser.find_element(By.XPATH, input_json[primary_owner][page_var]["age"][
        random.randint(0,len(input_json[primary_owner][page_var]["age"])-1)
    ])
    radio_box.click()

    radio_box = browser.find_element(By.XPATH, input_json[primary_owner][page_var]["occupation"][
        random.randint(0, len(input_json[primary_owner][page_var]["occupation"])-1)
    ])
    radio_box.click()


    radio_box = browser.find_element(By.XPATH, input_json[primary_owner][page_var]["income"][
        random.randint(0, len(input_json[primary_owner][page_var]["income"])-1)
    ])
    radio_box.click()

    

    submit_button = browser.find_element(By.XPATH,input_json[primary_owner][page_var]["submit"])
    submit_button.click()

    #third page

    time.sleep(2)
    browser.implicitly_wait(2)

    page_var = "thirdPage"

    radio_box = browser.find_element(By.XPATH, input_json[primary_owner][page_var]["ques1"][
        random.randint(0,len(input_json[primary_owner][page_var]["ques1"])-1)
    ])
    radio_box.click()


    radio_box = browser.find_element(By.XPATH, input_json[primary_owner][page_var]["ques2"][
        random.randint(0,len(input_json[primary_owner][page_var]["ques2"])-1)
    ])
    radio_box.click()

    text_box = browser.find_element(By.XPATH, input_json[primary_owner][page_var]["ques3"])
    text_box.send_keys(str(random.randint(200,500)))
    matrix = input_json[primary_owner][page_var]["matrix"]
    for i in range(17):
        option_id = matrix[i][
            random.randint(0,len(matrix[i])-1)
        ]
        radio_box = browser.find_element(By.XPATH, option_id)
        radio_box.click()

    submit_button = browser.find_element(By.XPATH,input_json[primary_owner][page_var]["submit"])
    submit_button.click()   




def demo():


    browser.get(input_json["demo"]["url"])

    browser.implicitly_wait(20)
    time.sleep(2)


    text_box_1 = browser.find_element(By.XPATH, input_json["demo"]["xPathTextBox1"])
    text_box_1.send_keys("siddharth")


    text_box_2 = browser.find_element(By.XPATH, input_json["demo"]["xPathTextBox2"])
    text_box_2.send_keys("siddharth 2")


    radio_box = browser.find_element(By.XPATH,input_json["demo"]["xPathRadioBox1"])
    radio_box.click()


    check_box = browser.find_element(By.XPATH,input_json["demo"]["xPathCheckBox"])
    check_box.click()


    submit_button = browser.find_element(By.XPATH,input_json["demo"]["xPathSubmit"])
    submit_button.click()


def wrapper_runner(required_numbers):
    names_list = getRequiredNames(required_numbers)
    random.shuffle(names_list)
    for name in names_list:
        shachi(name)
        time.sleep(2)

wrapper_runner(1)
time.sleep(5)