Python Forum

Full Version: Instagram Bot _ Posting Comments
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Guys,

I have put together a basic bot that opens my instagram and comments on the first photo. I have managed to make it scroll down in random intervals but am not having much success in making it find and comment of additional photos. Any ideas on how to condense this code into a single action that repeats a number of times would help. Part of my code is below.

comment = "Awesome!"

driver.find_element_by_xpath("//textarea[@aria-label='Add a comment…']").click()

sleep(4)

driver.find_element_by_xpath("//textarea[@aria-label='Add a comment…']").send_keys(comment)

sleep(4)

driver.find_element_by_xpath("//button[contains(.,'Post')]").click()

sleep(4)

driver.execute_script("window.scrollTo(0, 2000);")

sleep(4)

driver.execute_script("window.scrollTo(0, 4000);")

sleep(4)

driver.execute_script("window.scrollTo(0, 6000);")

sleep(4)

print('Finished') 
You can use instapy and that can make it all so much easy for you.Another approach can be using pyautogui.You can use it to locate the comment icon on the page and click it.
Intapy API was discontinued.
Tried to set up a for loop using selenium and the pyautogui.
If the code can not find ("//textarea[@aria-label='Add a comment…']") it crashes. If I # that line the scroll works fine.
I'll keep working on a solution. Any help would be great so that if the code can not find that line if loops back to the scroll.

for z in range(0,10):
    pyautogui.scroll(-1000)
    driver.find_element_by_xpath("//textarea[@aria-label='Add a comment…']")
    if True:
        driver.find_element_by_xpath("//textarea[@aria-label='Add a comment…']").click()
        driver.find_element_by_xpath("//textarea[@aria-label='Add a comment…']").send_keys(comment)
        driver.find_element_by_xpath("//button[contains(.,'Post')]").click()
    else:
        Print("nothing")
    time.sleep(2)