---
# System prepended metadata

title: test code

---

# test code

```
@router.get("/autolike/{account}/{password}/{minWaitTime}/{maxWaitTime}/{hashtag}/{maxLike}", response_description="exec auto lilke")
async def auto_like(account: str, password: str, minWaitTime: int, maxWaitTime: int, hashtag: str, maxLike: int):
    options = Options()
    options.add_argument("--disable-notifications")
    options.add_argument("--start-maximized")  # max. the window size
    # options.add_argument('--headless')

    # Have to edit the path of the chrome driver
    driver = webdriver.Chrome(
        '/Users/twlin/code/iglike_framework/backend/apps/todo/chromedriver', chrome_options=options)
    action = webdriver.ActionChains(driver)

    driver.get("https://www.instagram.com/")

    # 抓取：帳號密碼輸入框
    try:
        account_textbox = WebDriverWait(driver, 10, 0.2).until(
            EC.presence_of_element_located((By.NAME, "username")), '-1')
        password_textbox = WebDriverWait(driver, 10, 0.2).until(
            EC.presence_of_element_located((By.NAME, "password")), '-1')
    except:
        print('帳號或密碼的 By.NAME 改變')
        driver.quit()
        return '-1'

    # 抓取：登入按鈕
    try:
        loginBtn = WebDriverWait(driver, 10, 0.2).until(
            EC.presence_of_element_located((By.XPATH, "//div[@class='_ab8w  _ab94 _ab99 _ab9f _ab9m _ab9p  _abak _abb8 _abbq _abb- _abcm']")), '-1')
    except:
        print('登入按鈕的 By.XPATH 改變')
        driver.quit()
        return '-2'

    # 輸入：帳號密碼
    # 執行：登入
    time.sleep(random.randint(minWaitTime, maxWaitTime))
    account_textbox.send_keys(account)
    time.sleep(random.randint(minWaitTime, maxWaitTime))
    password_textbox.send_keys(password)
    print('輸入完帳號密碼，等待：', minWaitTime, '~', maxWaitTime, '秒，就執行登入')
    time.sleep(random.randint(minWaitTime, maxWaitTime))
    loginBtn.click()
    print('已按下登入')

    '''
        按下登入按鈕後，若:
        1. 帳號密碼錯誤
        2. 密碼 < 6個字元
        則10秒後才 return 400
    '''

    try:
        # 抓取：稍後再說按鈕
        # 沒抓到代表沒登入成功
        later = WebDriverWait(driver, 10, 0.2).until(EC.presence_of_element_located(
            (By.XPATH, "//div[@class='_ac8f']//button")), '-1')
        print('已找到`稍後再說`按鈕，等待：', minWaitTime, '~', maxWaitTime, '秒，就按稍後再說')
    except:
        print("帳號或密碼不正確")
        driver.quit()
        return '400'

    # 執行：按下稍後再說
    time.sleep(random.randint(minWaitTime, maxWaitTime))
    later.click()
    print('已按下`稍後再說`，等待：', minWaitTime, '~', maxWaitTime, '秒，就找hashtag')

    # 找 hashtag
    time.sleep(random.randint(minWaitTime, maxWaitTime))
    driver.get("https://www.instagram.com/explore/tags/" + hashtag)

    time.sleep(random.randint(minWaitTime, maxWaitTime))

    #
    # 先往下拉，試圖抓到貼文
    for i in range(5):
        driver.find_element(By.TAG_NAME, 'body').send_keys(Keys.SPACE)
        time.sleep(random.randint(minWaitTime, maxWaitTime))

    # 抓到目前所能見的貼文們
    post_count = driver.find_elements(
        By.XPATH, "//div[@class='_aabd _aa8k _aanf']")
    print("目前抓到：", len(post_count), "篇貼文")

    liked = 0
    # 隨機按 目前所有貼文/2 篇貼文的讚
    for round in range(int(len(post_count)/2)):
        time.sleep(random.randint(minWaitTime, maxWaitTime))
        # 點進某篇貼文
        post_count[random.randint(0, len(post_count) - 1)].click()
        time.sleep(random.randint(minWaitTime, maxWaitTime))
        # 按讚
        try:
            likeBtn = driver.find_element(
                By.XPATH, "//section[@class='_aamu _ae3_ _ae47 _ae48']//span[@class='_aamw']//button[@class='_abl-']//div[@class='_abm0 _abm1']")
            time.sleep(random.randint(minWaitTime, maxWaitTime))
            likeBtn.click()
            liked = liked + 1
            print('按了第 ', liked, ' 篇讚')
            time.sleep(random.randint(minWaitTime, maxWaitTime))
        except:
            print("按過，故略")

        # if likeBtn.get_attribute('aria-label') == '讚':
        #     likeBtn.click()
        #     liked = liked + 1
        #     print('按了第 ', liked, ' 篇讚')
        # else:
        #     print('此篇已按過讚，故略過')

        webdriver.ActionChains(driver).send_keys(Keys.ESCAPE).perform()

    time.sleep(20)
    driver.quit()
```