Beispiel #1
0
def create_account(username, password, email, birthday, captchakey2,
                   captchatimeout):
    if password:
        _validate_password(password)

    print("Attempting to create user {user}:{pw}. Opening browser...".format(
        user=username, pw=password))
    if captchakey2:
        dcap = dict(DesiredCapabilities.PHANTOMJS)
        dcap[
            "phantomjs.page.settings.userAgent"] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'
        driver = PhantomJS(desired_capabilities=dcap)
    else:
        driver = Chrome()
        driver.set_window_size(600, 600)

    # Input age: 1992-01-08
    print("Step 1: Verifying age using birthday:", birthday)
    driver.get("{}/sign-up/".format(BASE_URL))
    assert driver.current_url == "{}/sign-up/".format(BASE_URL)
    elem = driver.find_element_by_name("dob")

    # Workaround for different region not having the same input type
    driver.execute_script(
        "var input = document.createElement('input'); input.type='text'; input.setAttribute('name', 'dob'); arguments[0].parentNode.replaceChild(input, arguments[0])",
        elem)

    elem = driver.find_element_by_name("dob")
    elem.send_keys(birthday)
    elem.submit()

    # Create account page
    print("Step 2: Entering account details")
    assert driver.current_url == "{}/parents/sign-up".format(BASE_URL)

    user = driver.find_element_by_name("username")
    user.clear()
    user.send_keys(username)

    _validate_username(driver, username)

    elem = driver.find_element_by_name("password")
    elem.clear()
    elem.send_keys(password)

    elem = driver.find_element_by_name("confirm_password")
    elem.clear()
    elem.send_keys(password)

    elem = driver.find_element_by_name("email")
    elem.clear()
    elem.send_keys(email)

    elem = driver.find_element_by_name("confirm_email")
    elem.clear()
    elem.send_keys(email)

    driver.find_element_by_id("id_public_profile_opt_in_1").click()
    driver.find_element_by_name("terms").click()

    if not captchakey2:
        #Do manual captcha entry
        print(
            "You did not pass a 2captcha key. Please solve the captcha manually."
        )
        elem = driver.find_element_by_class_name("g-recaptcha")
        driver.execute_script("arguments[0].scrollIntoView(true);", elem)
        # Waits 90 seconds for you to input captcha
        try:
            WebDriverWait(driver, 90).until(
                EC.text_to_be_present_in_element_value(
                    (By.NAME, "g-recaptcha-response"), ""))
            print("Captcha successful.")
        except TimeoutException:
            print("Timed out while manually solving captcha")
    else:
        # Now to automatically handle captcha
        print("Starting autosolve recaptcha")
        html_source = driver.page_source
        gkey_index = html_source.find(
            "https://www.google.com/recaptcha/api2/anchor?k=") + 47
        gkey = html_source[gkey_index:gkey_index + 40]
        recaptcharesponse = "Failed"
        while (recaptcharesponse == "Failed"):
            recaptcharesponse = openurl("http://2captcha.com/in.php?key=" +
                                        captchakey2 +
                                        "&method=userrecaptcha&googlekey=" +
                                        gkey + "&pageurl=" +
                                        driver.current_url)
        captchaid = recaptcharesponse[3:]
        recaptcharesponse = "CAPCHA_NOT_READY"
        elem = driver.find_element_by_class_name("g-recaptcha")
        print("We will wait 10 seconds for captcha to be solved by 2captcha")
        start_time = time.monotonic()
        timedout = False
        while recaptcharesponse == "CAPCHA_NOT_READY":
            time.sleep(10)
            elapsedtime = time.monotonic() - start_time
            if elapsedtime > captchatimeout:
                print("Captcha timeout reached. Exiting.")
                timedout = True
                break
            print("Captcha still not solved, waiting another 10 seconds.")
            recaptcharesponse = "Failed"
            while (recaptcharesponse == "Failed"):
                recaptcharesponse = openurl(
                    "http://2captcha.com/res.php?key={}&action=get&id={}".
                    format(captchakey2, captchaid))
        if timedout == False:
            solvedcaptcha = recaptcharesponse[3:]
            elem = driver.find_element_by_name("g-recaptcha-response")
            elem = driver.execute_script(
                "arguments[0].style.display = 'block'; return arguments[0];",
                elem)
            elem.send_keys(solvedcaptcha)
            print("Solved captcha")
    try:
        user.submit()
    except StaleElementReferenceException:
        print("Error StaleElementReferenceException!")

    try:
        _validate_response(driver)
    except Exception:
        print("Failed to create user:"******"Account successfully created.")
    driver.quit()
    return True
Beispiel #2
0
def create_account(username, password, email, birthday, captchakey2, captchatimeout):
    if password is not None:
        _validate_password(password)

    print("Attempting to create user {user}:{pw}. Opening browser...".format(user=username, pw=password))
    if captchakey2 != None:
        dcap = dict(DesiredCapabilities.PHANTOMJS)
        dcap["phantomjs.page.settings.userAgent"] = user_agent
        #driver = webdriver.PhantomJS(desired_capabilities=dcap)
        driver = PhantomJS(desired_capabilities=dcap)
    else:
        #driver = webdriver.Chrome()
        driver = Chrome()
        driver.set_window_size(600, 600)

    # Input age: 1992-01-08
    print("Step 1: Verifying age using birthday: {}".format(birthday))
    driver.get("{}/sign-up/".format(BASE_URL))
    assert driver.current_url == "{}/sign-up/".format(BASE_URL)
    elem = driver.find_element_by_name("dob")

    # Workaround for different region not having the same input type
    driver.execute_script("var input = document.createElement('input'); input.type='text'; input.setAttribute('name', 'dob'); arguments[0].parentNode.replaceChild(input, arguments[0])", elem)

    elem = driver.find_element_by_name("dob")
    elem.send_keys(birthday)
    elem.submit()
    # Todo: ensure valid birthday

    # Create account page
    print("Step 2: Entering account details")
    assert driver.current_url == "{}/parents/sign-up".format(BASE_URL)

    user = driver.find_element_by_name("username")
    user.clear()
    user.send_keys(username)

    _validate_username(driver, username)

    elem = driver.find_element_by_name("password")
    elem.clear()
    elem.send_keys(password)

    elem = driver.find_element_by_name("confirm_password")
    elem.clear()
    elem.send_keys(password)

    elem = driver.find_element_by_name("email")
    elem.clear()
    elem.send_keys(email)

    elem = driver.find_element_by_name("confirm_email")
    elem.clear()
    elem.send_keys(email)

    driver.find_element_by_id("id_public_profile_opt_in_1").click()
    driver.find_element_by_name("terms").click()

    if captchakey2 == None:
        #Do manual captcha entry
        print("You did not pass a 2captcha key. Please solve the captcha manually.")
        elem = driver.find_element_by_class_name("g-recaptcha")
        driver.execute_script("arguments[0].scrollIntoView(true);", elem)
        # Waits 1 minute for you to input captcha
        try:
            WebDriverWait(driver, 60).until(EC.text_to_be_present_in_element_value((By.NAME, "g-recaptcha-response"), ""))
            print("Captcha successful. Sleeping for 1 second...")
            time.sleep(1)
        except TimeoutException, err:
            print("Timed out while manually solving captcha")