简体   繁体   中英

Creating Instagram Bot using selenium library

So I am trying to create insta bot that opens up a specific hashtag in search bar and then navigates to it. I have been having a problem to navigate bot to search bar it always tells me that path is not able to be found, any ideas how can I make bot target search bar and send hashtag keys to it? here is my code:

from selenium import webdriver
from time import sleep 
from insta import username,password,hashtag

class InstaBot():
def __init__(self):
    self.driver = webdriver.Chrome()

def login(self):
    self.driver.get('https://www.instagram.com/')

    sleep(3)

    #loggin in to instagram with facebook
    fb_btn = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/main/article/div[2]/div[1]/div/form/div[1]/button')
    fb_btn.click()

    #logging into acc
    email = self.driver.find_element_by_xpath('//*[@id="email"]')
    email.send_keys(username)

    pswd = self.driver.find_element_by_xpath('//*[@id="pass"]')
    pswd.send_keys(password)

    login_btn = self.driver.find_element_by_xpath('//*[@id="loginbutton"]')
    login_btn.click()
    sleep(4)

    self.driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]")\
        .click()

    #navigating to search bar and sending hashtag into it
    hashtag = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[1]/a/svg')
    hashtag.send_keys(hashtag)

You can navigate to the search bar by executing JavaScript:

SCRIPT = f"document.getElementsByClassName('XTCLo x3qfX')[0].value = '{SEARCH_VALUE}'"
driver.execute_script(SCRIPT)

However, if you just want to go to a hashtag page, I would recommend using driver.get(f"https://www.instagram.com/explore/tags/{HASHTAG}/")

you can direct send the page,for ex:

https://www.instagram.com/explore/tags/{hastags}/

so

when you run the program sometimes ıt can click different buttons ıdk why when ı made the same program ıt does that :) when you directly send the lınk for hastags ıt goes there even the program click different buttons

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM