python - 使用 selenium 库创建 Instagram Bot

标签 python python-3.x selenium instagram bots

所以我正在尝试创建 insta 机器人,它在搜索栏中打开特定的主题标签,然后导航到它。我在将机器人导航到搜索栏时遇到问题,它总是告诉我无法找到路径,有什么想法如何让机器人瞄准搜索栏并向其发送主题标签键吗?这是我的代码:

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)

最佳答案

您可以通过执行 JavaScript 导航到搜索栏:

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

但是,如果您只想转到主题标签页面,我建议使用 driver.get(f"https://www.instagram.com/explore/tags/{HASHTAG}/")

关于python - 使用 selenium 库创建 Instagram Bot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60033950/

相关文章:

python - Python 中的 Mechanize - 提交后重定向不起作用

python - Django - 查看 JSON login_required

android - 如何将 Kivy 应用部署到 Google 应用商店?

python-3.x - 如何使用Scikit learn CountVectorizer?

python - Django 干草堆测试

python - 在 Python 中显示 Blob 图像 (App Engine)

python - 单击时发出 QTableWidgetItem 特定信号

Selenium、FluentAutomation 和 NUnit - 如何为每个测试用例切换浏览器?

xml - 无法使用 xpath == "//*[text()[contains(.,' "+ word + " ')]]"找到元素

maven - 在 Maven Cucumber 报告中构建失败时不会生成报告