Search code examples
python-3.xseleniumselenium-webdriverwebdriverstrftime

Add a unique timestamp to a text using Selenium and Python3


Quick question which I thought I'd find a simple answer to but so far, no luck.

I'm creating automation to go through sign up forms and for this I ideally need to send some unique text. I was thinking of something like name+timestamp (as do-able on Ghost Inspector).

Currently I'm okay just writing a quick unique name each time in my code (using send.keys('')), just looking to see if there's a way to cut this small chore out really.


Solution

  • To add a unique timestamp to a text you can use Python's strftime() method from time module as follows:

    • Code Block:

      from selenium import webdriver
      from time import gmtime, strftime
      
      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      options.add_argument('disable-infobars')
      driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("http://www.google.com")
      driver.find_element_by_name("q").send_keys("James Stott{}".format(strftime("%Y-%m-%d %H:%M:%S", gmtime())))
      
    • Browser Snapshot:

    name_timestamp_strftime