0

I wrote some codes and I want it to run 10 times. I tried to find ways to do it from the internet, but I couldn't figure it out how to apply them in my code.

from selenium import webdriver
import time
import random
import string

def random_string(length=32, uppercase=True, lowercase=True, numbers=True):
    character_set = ''

    if uppercase:
        character_set += string.ascii_uppercase
    if lowercase:
        character_set += string.ascii_lowercase
    if numbers:
        character_set += string.digits

    return ''.join(random.choice(character_set) for i in range(length))

my_random = random_string(length=13, uppercase=False) + '@gmail.com'
my_pw = random_string(length=30, uppercase=True, numbers=True)

with open('C:\\m2sifre\\demo2.txt', 'a') as fh:
    fh.write(f'{my_random}\n {my_pw}\n')

browser = webdriver.Firefox()

browser.get("https://gameforge.com/tr-TR/sign-up")
# //*[@id="root"]/section/div/div/div/div/div/form/fieldset/footer/div[2]/button/span/p
time.sleep (2)

username = browser.find_element_by_name("username")
password = browser.find_element_by_name("password")

username.send_keys(my_random)
password.send_keys(my_pw)

kayit_ol = browser.find_element_by_xpath("//*[@id='root']/section/div/div/div/div/div/form/fieldset/footer/div[2]/button/span/p")

kayit_ol.click()
time.sleep (2)

browser.close()
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Does this answer your question? [python how to repeat code](https://stackoverflow.com/questions/42747894/python-how-to-repeat-code) ; https://stackoverflow.com/questions/42809421/how-to-repeat-a-function-in-python-complete-beginner-first-lines-of-code-ever ; https://stackoverflow.com/questions/43268186/python-repeating-code-with-a-while-loop – Tomerikoo Oct 21 '20 at 10:25

1 Answers1

0

throw it in a for loop:

from selenium import webdriver
import time
import random
import string

def random_string(length=32, uppercase=True, lowercase=True, numbers=True):
    character_set = ''

    if uppercase:
        character_set += string.ascii_uppercase
    if lowercase:
        character_set += string.ascii_lowercase
    if numbers:
        character_set += string.digits

    return ''.join(random.choice(character_set) for i in range(length))

for i in range(10):
    my_random = random_string(length=13, uppercase=False) + '@gmail.com'
    my_pw = random_string(length=30, uppercase=True, numbers=True)
    with open('C:\\m2sifre\\demo2.txt', 'a') as fh:
        fh.write(f'{my_random}\n {my_pw}\n')
    browser = webdriver.Firefox()
    browser.get("https://gameforge.com/tr-TR/sign-up")
    
    # //*[@id="root"]/section/div/div/div/div/div/form/fieldset/footer/div[2]/button/span/p
    time.sleep (2)
    username = browser.find_element_by_name("username")
    password = browser.find_element_by_name("password")
    username.send_keys(my_random)
    password.send_keys(my_pw)
    kayit_ol = browser.find_element_by_xpath("//*[@id='root']/section/div/div/div/div/div/form/fieldset/footer/div[2]/button/span/p")
    kayit_ol.click()
    time.sleep (2)
    browser.close()
Ironkey
  • 2,568
  • 1
  • 8
  • 30
  • how to select amount of time to repeat in while? thanks for answer – Baran Arslan Oct 20 '20 at 23:13
  • i just copy pasted to place you showed and gives an error ERROR expected an indented block – Baran Arslan Oct 20 '20 at 23:18
  • that's because your code needs to be indented properly, it needs to be inside the loop with correct indentation. I have edited with what looks like correct indentation, hopefully that will help – Ironkey Oct 20 '20 at 23:19
  • Im really sorry for bothering but im so new at this just started 5, 6 hours ago. In youtube i watch tutorials and do the loop in small codes but in mine i cant do it. If it won't be a problem, can you fix it and share it. Thank you – Baran Arslan Oct 20 '20 at 23:24
  • the updated code works (kinda?) the website asks for you to accept cookies and the ide im using to run it uses a new VM each time. if the original code worked for you then what i have edited in should work as well. – Ironkey Oct 20 '20 at 23:32
  • 1
    yes worked very well thank you soo much for your help and time <3 – Baran Arslan Oct 20 '20 at 23:43