stavros-melidoniotis / ig-tagger

Python script that automates the process of tagging people in Instagram giveaways.
13 stars 3 forks source link

Running script getting errors #5

Closed iChristosK closed 1 year ago

iChristosK commented 1 year ago

I am getting below errors while followings all the instructions and running inside ig-tagger directory and using command python3 ig_tagger.py


python3 ig_tagger.py
/Users/christos/Developer/Backend/Python/ig-tagger/ig_tagger.py:52: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  chrome = webdriver.Chrome(ChromeDriverManager().install())
Traceback (most recent call last):
  File "/Users/christos/Developer/Backend/Python/ig-tagger/ig_tagger.py", line 58, in <module>
    cookie_accept = chrome.find_element(By.XPATH, COOKIE_XPATH)
  File "/Users/christos/Developer/Backend/Python/ig-tagger/environment/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 861, in find_element
    return self.execute(Command.FIND_ELEMENT, {"using": by, "value": value})["value"]
  File "/Users/christos/Developer/Backend/Python/ig-tagger/environment/lib/python3.10/site-packages/selenium/webdriver/remote/webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "/Users/christos/Developer/Backend/Python/ig-tagger/environment/lib/python3.10/site-packages/selenium/webdriver/remote/errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/button[1]"}
  (Session info: chrome=108.0.5359.98)

Stacktrace:
0   chromedriver                        0x00000001025c3b7c chromedriver + 4209532
1   chromedriver                        0x000000010254f418 chromedriver + 3732504
2   chromedriver                        0x0000000102202368 chromedriver + 271208
3   chromedriver                        0x000000010223dfc8 chromedriver + 516040
4   chromedriver                        0x0000000102275804 chromedriver + 743428
5   chromedriver                        0x000000010223059c chromedriver + 460188
6   chromedriver                        0x0000000102231644 chromedriver + 464452
7   chromedriver                        0x000000010259530c chromedriver + 4018956
8   chromedriver                        0x0000000102598fd8 chromedriver + 4034520
9   chromedriver                        0x000000010259f11c chromedriver + 4059420
10  chromedriver                        0x0000000102599bac chromedriver + 4037548
11  chromedriver                        0x0000000102573bb0 chromedriver + 3881904
12  chromedriver                        0x00000001025b6120 chromedriver + 4153632
13  chromedriver                        0x00000001025b6274 chromedriver + 4153972
14  chromedriver                        0x00000001025ca82c chromedriver + 4237356
15  libsystem_pthread.dylib             0x00000001af72e06c _pthread_start + 148
16  libsystem_pthread.dylib             0x00000001af728e2c thread_start + 8
stavros-melidoniotis commented 1 year ago

Hey @iChristosK, I managed to reproduce the issue you're facing and also provide a solution. Please replace the ig_tagger.py file contents with the following code:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

from configparser import ConfigParser

import signal
import time
import random

LOGIN_XPATH = '//*[@id="loginForm"]/div/div[3]/button'
LOGGED_XPATH = '/html/body/div[2]/div/div/div/div[1]/div/div/div/div[1]/section/main/div/div/div/div/button'
USERNAME_XPATH = '//*[@id="loginForm"]/div/div[1]/div/label/input'
PASS_XPATH = '//*[@id="loginForm"]/div/div[2]/div/label/input'
COMMENT_XPATH = '/html/body/div[2]/div/div/div/div[1]/div/div/div/div[1]/section/main/div[1]/div/article/div/div[2]/div/div[2]/section[3]/div/form/textarea'
POST_XPATH = '/html/body/div[2]/div/div/div/div[1]/div/div/div/div[1]/section/main/div[1]/div/article/div/div[2]/div/div[2]/section[3]/div/form/div[2]/div'
COOKIE_XPATH = '/html/body/div[2]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div[2]/div/button[1]'

def choose_names_to_tag(ig_names, required_tags):
    tags = []

    while len(tags) < int(required_tags):
        name = random.choice(ig_names)

        if name not in tags:
            tags.append(name)

    return tags

def signal_handler(*args):
    print("\n\nTotal tags made: {}".format(number_of_tags))
    exit(0)

if __name__ == "__main__":
    number_of_tags = 0
    signal.signal(signal.SIGINT, signal_handler)

    parser = ConfigParser()
    parser.read('config.ini', encoding='utf8')

    contest_url = parser.get('Instagram', 'Contest')
    ig_names = parser.get('Instagram', 'Names').replace('\n', '').split(',')
    required_tags = parser.get('Instagram', 'Tags')
    username = parser.get('Instagram', 'Username')
    password = parser.get('Instagram', 'Password')

    chrome = webdriver.Chrome(ChromeDriverManager().install())
    chrome.get("https://www.instagram.com")

    WebDriverWait(chrome, 15).until(
        EC.presence_of_element_located((By.XPATH, USERNAME_XPATH)))

    time.sleep(1)

    cookie_accept = chrome.find_element(By.XPATH, COOKIE_XPATH)
    username_input = chrome.find_element(By.XPATH, USERNAME_XPATH)
    password_input = chrome.find_element(By.XPATH, PASS_XPATH)
    login_button = chrome.find_element(By.XPATH, LOGIN_XPATH)

    cookie_accept.click()
    username_input.send_keys(username)
    password_input.send_keys(password)

    time.sleep(1)
    login_button.click()

    WebDriverWait(chrome, 15).until(
        EC.presence_of_element_located((By.XPATH, LOGGED_XPATH)))

    chrome.get(contest_url)

    WebDriverWait(chrome, 15).until(
        EC.presence_of_element_located((By.XPATH, COMMENT_XPATH)))

    while True:
        names = choose_names_to_tag(ig_names, required_tags)
        comment_input = chrome.find_element(By.XPATH, COMMENT_XPATH)
        comment_input.click()
        comment_input = chrome.find_element(By.XPATH, COMMENT_XPATH)

        comment = ""
        for name in names:
            comment += "{} ".format(name)

        comment_input.send_keys(comment)
        post_button = chrome.find_element(By.XPATH, POST_XPATH)
        post_button.click()

        time.sleep(2)

        waiting_to_unblock = True

        while waiting_to_unblock:
            try:
                # if element is present then IG blocked comments
                chrome.find_element_by_class_name("HGN2m")

                print("blocked")

                # wait 1 min before commenting again
                time.sleep(60)
                post_button = chrome.find_element(By.XPATH, POST_XPATH).click()
                time.sleep(2)
            except:
                waiting_to_unblock = False
                number_of_tags += 1

                # set a random waiting time to mess with IG algorithm
                seconds_to_wait = random.randint(1, 60)
                print("Tagged: {}\nWaiting for {} seconds\n".format(
                    comment, seconds_to_wait))
                time.sleep(seconds_to_wait)

The reason why I'm not updating the master branch with the above solution is because of other users mentioning that this version worked for them. If you still face any issues with the script I'd be happy to help!

iChristosK commented 1 year ago

Thanks for your response Stavros.

Now I get the following error for webdriver

Traceback (most recent call last): File "/Users/christos/Developer/Backend/Python/ig-tagger/ig_tagger.py", line 52, in <module> chrome = webdriver.Chrome(ChromeDriverManager().install()) NameError: name 'webdriver' is not defined

stavros-melidoniotis commented 1 year ago

Have you installed all the required packages for the script to run? If not, then you need to install them by running pip install -r requirements.txt. I would also advise you to create a python venv first and install the packages there, to avoid any conflicts on your system. You can find a small guide on the Requirements section of the project's readme.

JoyL-new commented 1 year ago

Hello Starvos, I seem to have encountered the same problem. I have tested out your new code on venv and got the same error for the webdriver. Requirement already satisfied upon reinstalling requirements.txt.

stavros-melidoniotis commented 1 year ago

Hi @JoyL-new, it was my fault as there was a missing import on the code block I pasted above. Please add from selenium import webdriver at the very top of the file and it should work like a charm. I've also updated the code block with the new import.

iChristosK commented 1 year ago

Thank you for your reply @stavros-melidoniotis , although your suggestion of adding from selenium import webdriver has solved the issue. I still encounter some issues as shown below. The script runs and successfully logs on to instagram. But it crashes after a few seconds.

Hope the below would help


/Users/christos/Developer/Backend/Python/ig-tagger/ig_tagger.py:53: DeprecationWarning: executable_path has been deprecated, please pass in a Service object
  chrome = webdriver.Chrome(ChromeDriverManager().install())
Traceback (most recent call last):
  File "/Users/christos/Developer/Backend/Python/ig-tagger/ig_tagger.py", line 73, in <module>
    WebDriverWait(chrome, 15).until(
  File "/Users/christos/Developer/Backend/Python/ig-tagger/envm/lib/python3.10/site-packages/selenium/webdriver/support/wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
0   chromedriver                        0x0000000104adbb7c chromedriver + 4209532
1   chromedriver                        0x0000000104a67418 chromedriver + 3732504
2   chromedriver                        0x000000010471a368 chromedriver + 271208
3   chromedriver                        0x0000000104755fc8 chromedriver + 516040
4   chromedriver                        0x000000010478d804 chromedriver + 743428
5   chromedriver                        0x000000010474859c chromedriver + 460188
6   chromedriver                        0x0000000104749644 chromedriver + 464452
7   chromedriver                        0x0000000104aad30c chromedriver + 4018956
8   chromedriver                        0x0000000104ab0fd8 chromedriver + 4034520
9   chromedriver                        0x0000000104ab711c chromedriver + 4059420
10  chromedriver                        0x0000000104ab1bac chromedriver + 4037548
11  chromedriver                        0x0000000104a8bbb0 chromedriver + 3881904
12  chromedriver                        0x0000000104ace120 chromedriver + 4153632
13  chromedriver                        0x0000000104ace274 chromedriver + 4153972
14  chromedriver                        0x0000000104ae282c chromedriver + 4237356
15  libsystem_pthread.dylib             0x000000019606506c _pthread_start + 148
16  libsystem_pthread.dylib             0x000000019605fe2c thread_start + 8
stavros-melidoniotis commented 1 year ago

@iChristosK have you tried rerunning the script after this error occurred? I wasn't able to reproduce so it may be caused randomly like e.g. when the page loads slower than usual etc.

iChristosK commented 1 year ago

Yes, I rerun and same issue.

stavros-melidoniotis commented 1 year ago

Could you please share a screenshot of your Instagram's home page? I suspect that they might have released a new layout which causes the element xpaths to be invalid.

iChristosK commented 1 year ago
Screenshot 2022-12-22 at 16 57 33
stavros-melidoniotis commented 1 year ago

@iChristosK there is indeed a different layout on your IG homepage than mine. I currently can't update the code to work on your version as I am not able to use this new layout yet, but will do as soon as it becomes available to me.