You can try to create a function for this as follows:
Just going over: times represents how many times your code will run and intermission the time period between each execution.
def Repetir_meu_código(vezes, intervalo):
for v in range(vezes):
driver.find_element_by_class_name("Ypffh").click()
comment_input_box = driver.find_element_by_class_name("Ypffh")
time.sleep(random.randint(2, 5))
self.type_like_a_person(random.choice(comments), comment_input_box)
time.sleep(random.randint(3, 5))
driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()
time.sleep(intervalo)
Repetir_meu_código(10, 60) # Repetir 10 vezes a cada 60 segundos
Or simply use the loop for out of function as well:
The v inside the for represents how many times the loop has rotated, its count starts from the number 0 and goes to the value defined in crease() least 1.
For example if the crease for 10 the last value of v will be 9 because in python the count starts from the 0. The name v can be exchanged for anything else like i for example or any other name.
for v in range(10):
driver.find_element_by_class_name("Ypffh").click()
comment_input_box = driver.find_element_by_class_name("Ypffh")
time.sleep(random.randint(2, 5))
self.type_like_a_person(random.choice(comments), comment_input_box)
time.sleep(random.randint(3, 5))
driver.find_element_by_xpath("//button[contains(text(), 'Publicar')]").click()
time.sleep(60)
Doing this process in model function your you have more control over your code and can simply call the function again to be executed at another time or even import your script to use the function in another code.
This way you only matter the chosen function:
from nome_do_script import função_escolhida
Import all:
from nome_do_script import *
Or simply import all also requiring specification within the code:
import nome_do_script
Here however in all functions or classes that you are importing need to specify for your current code the source of function or class
nome_do_script_importado.função_importada()
Note that there is no need to include terminations (.py, . pyw and etc.) in import.
Dude, the only way to repeat this code is with the loops
while
orfor
. What exactly was your problem using them ?– JeanExtreme002
I know it’s a bot. But my question is your script will just run this code repeatedly every minute or it will perform another activity while every minute it repeats this code?
– Augusto Vasques