Skip to content

Instantly share code, notes, and snippets.

@Glyphack
Created January 20, 2019 11:23
Show Gist options
  • Save Glyphack/ea47aaf10efc5db837828b2740f93a0e to your computer and use it in GitHub Desktop.
Save Glyphack/ea47aaf10efc5db837828b2740f93a0e to your computer and use it in GitHub Desktop.
auto instagram comment bot
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from time import sleep
from ast import literal_eval
import random
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException
from selenium.common.exceptions import TimeoutException
username = ""
password = ""
def click_element(browser, element, tryNum=0):
# There are three (maybe more) different ways to "click" an element/button.
# 1. element.click()
# 2. element.send_keys("\n")
# 3. browser.execute_script("document.getElementsByClassName('" +
# element.get_attribute("class") + "')[0].click()")
# I'm guessing all three have their advantages/disadvantages
# Before committing over this code, you MUST justify your change
# and potentially adding an 'if' statement that applies to your
# specific case. See the following issue for more details
# https://github.com/timgrossmann/InstaPy/issues/1232
# explaination of the following recursive function:
# we will attempt to click the element given, if an error is thrown
# we know something is wrong (element not in view, element doesn't
# exist, ...). on each attempt try and move the screen around in
# various ways. if all else fails, programmically click the button
# using `execute_script` in the browser.
try:
# use Selenium's built in click function
element.click()
# update server calls after a successful click by selenium
except Exception:
# click attempt failed
# try something funky and try again
if tryNum == 0:
# try scrolling the element into view
browser.execute_script(
"document.getElementsByClassName('" + element.get_attribute(
"class") + "')[0].scrollIntoView({ inline: 'center' });")
elif tryNum == 1:
# well, that didn't work, try scrolling to the top and then
# clicking again
browser.execute_script("window.scrollTo(0,0);")
elif tryNum == 2:
# that didn't work either, try scrolling to the bottom and then
# clicking again
browser.execute_script(
"window.scrollTo(0,document.body.scrollHeight);")
else:
# try `execute_script` as a last resort
# print("attempting last ditch effort for click, `execute_script`")
browser.execute_script(
"document.getElementsByClassName('" + element.get_attribute(
"class") + "')[0].click()")
# update server calls after last click attempt by JS
# end condition for the recursive function
return
# update server calls after the scroll(s) in 0, 1 and 2 attempts
# sleep for 1 second to allow window to adjust (may or may not be
# needed)
sleep_actual(1)
tryNum += 1
# try again!
click_element(browser, element, tryNum)
def get_comment_input(browser):
comment_input = browser.find_elements_by_xpath(
'//textarea[@placeholder = "Add a comment…"]')
if len(comment_input) <= 0:
comment_input = browser.find_elements_by_xpath(
'//input[@placeholder = "Add a comment…"]')
return comment_input
def open_comment_section(browser):
comment_elem = browser.find_elements_by_xpath(
"//button/span[@aria-label='Comment']")
if len(comment_elem) > 0:
try:
click_element(browser, comment_elem[0])
except WebDriverException:
print('comment nadarim')
else:
print('comment nadarim')
driver = webdriver.Chrome(executable_path="./chromedriver")
driver.get("https://instagram.com/accounts/login/?force_classic_login")
input_username = driver.find_elements_by_xpath("//input[@name='username']")[0]
input_password = driver.find_elements_by_xpath("//input[@name='password']")[0]
input_username.send_keys(username)
input_password.send_keys(password)
input_password.send_keys(Keys.RETURN)
# login
sleep(8)
driver.get("https://www.instagram.com/p//") # post
# go to the post
# random.shuffle(users_list, random=None)
print('start commenting')
# split users in a list
for i in range(1100, 1500):
if (i % 10 == 0):
print('sleep')
sleep(random.randint(470, 630))
else:
open_comment_section(driver)
comment_input = get_comment_input(driver)
if len(comment_input) > 0:
comment_to_be_sent = f"@{users_list.pop(i)}" + " "
comment_input[0].clear()
comment_input = get_comment_input(driver)
driver.execute_script(
"arguments[0].value = arguments[1];",
comment_input[0], comment_to_be_sent)
comment_input[0].send_keys('\b')
# comment_input[0].send_keys(comment_to_be_sent)
comment_input[0].submit()
print('commented', i)
sleep_time = random.randint(280, 380)
sleep(sleep_time)
else:
print('cannot find comments section')
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment