mardi 19 janvier 2021

How to randomize functions inside a list?

I've written a script that logs in to instagram using Selenium, searches for a tag and clicks on the 3 first pictures from left to right.

It first opens the first picture from left to right at the first row, then likes the picture, and then closes it and moves to the next one.

For each picture I wrote a function Pic_1(), Pic_2(), Pic_(3). And placed it inside a list.

Then I use the shuffle method in order to avoid getting spoted by Instagram. However, when I use the shuffle method (I also tried with choice) it still goes from picture 1 to 3 and don't randomize as I'd like.

def Pic_1():
    #Picture 1 from Left to Right
    search_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div'))
        ).click()
    time.sleep(1)
    #Like Button <3
    search_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button'))
        ).click()
    #Close Picture Button
    search_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[3]/button'))
        ).click()
def Pic_2():
    #Picture 2 from Left to Right
    search_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[2]'))
        ).click()
    time.sleep(1)
    #Like Button <3
    search_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button'))
        ).click()
    #Close Picture Button
    search_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[3]/button'))
        ).click()
def Pic_3():
    #Picture 3 from Left to Right
    search_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/main/article/div[1]/div/div/div[1]/div[3]'))
        ).click()
    time.sleep(1)
    #Like Button <3
    search_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[2]/div/article/div[3]/section[1]/span[1]/button'))
        ).click()
    #Close Picture Button
    search_element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '/html/body/div[5]/div[3]/button'))
        ).click()

import random

mylist = [Pic_1(), Pic_2(), Pic_3()]

print(random.shuffle(mylist)

Does anyone know how I can solve this?




Aucun commentaire:

Enregistrer un commentaire