I am completely new to development and automation so I've been playing around with Python and Selenium to fill out simple forms. The form I am using will detect existing email addresses in a Database so I need to know how to tell the program to use a unique email address on each automation test start. In this case, the email I want to use is huks214+(Randomvalue)@gmail.com and have that random value change each time I run the script. Below is a code snipit from Selenium

driver.find_element_by_id("Email_Address").send_keys("[email protected]")
driver.find_element_by_id("Cnfm_Email_Address").send_keys("[email protected]")

This is inputting the email address as I entered it on the form when I recorded it. I have the below code which will change the email address but I'm unsure on how to integrate it into the above script

import string
import random
email = 'huks214+' + ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(10)) + '@gmail.com'
print (email)

1 Answer 1


Just use the generated value instead of a hardcoded string. Extracting the email generation code into a function would also be a good idea:

import string
import random

def generate_email(prefix='huks214+', domain='gmail.com'):
    random_part = ''.join(random.choice(string.ascii_lowercase + string.digits) 
                          for _ in range(10))

    return prefix + random_part + '@' + domain

# your code that defines driver here
email = generate_email()

confirm_email = driver.find_element_by_id("Cnfm_Email_Address")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.