I’m trying to put a timer in my python code

Asked

Viewed 93 times

0

In this code I intend to put a timer before each click, this timer needs to wait 2 to 5 seconds and the time should be random, for example 2.4 seconds, 3.1 seconds, always a random time before each click. how would you guys do this? I’m young and having a hard time ;-;

def compra_item(self, driver, link):
        self.logger.info("Abrindo.")
        driver.get(link)
        self.logger.info("Clicando...")
        driver.find_element_by_css_selector(".btn.btn_big.color").click()
        self.logger.info("Confirmando...")
        driver.find_element_by_css_selector("button[type=submit]").click()

        main_text = driver.page_source
        if "flashNoticeInfo" in main_text:
            msg = driver.find_element_by_id('flashNoticeInfo').text
            self.logger.info(msg)
        self.logger.info("Retornando.")

1 answer

1

The easiest way is to use a delay in conjunction with a random time.

import time
from random import uniform

numero_aleatorio = uniform(2,5)) #Gera um numero aleatório entre 2 e 5
time.sleep(numero_aleatorio )   # Aplica um delay com base no número aleatório

Browser other questions tagged

You are not signed in. Login or sign up in order to post.