Closed iChristosK closed 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!
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
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.
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.
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.
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
@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.
Yes, I rerun and same issue.
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 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.
I am getting below errors while followings all the instructions and running inside ig-tagger directory and using command
python3 ig_tagger.py