【问题标题】:Creating Instagram Bot using selenium library使用 selenium 库创建 Instagram Bot
【发布时间】:2020-02-03 06:12:26
【问题描述】:

所以我正在尝试创建在搜索栏中打开特定主题标签然后导航到它的 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)

【问题讨论】:

  • insta 是分开的 .py 文件,我有我的用户信息
  • 尝试使用//input[@placeholder='Search'] xpath。
  • 它有效,但目前它没有将键发送到搜索栏中给出 TypeError: object of type 'WebElement' has no len()

标签: python python-3.x selenium instagram bots


【解决方案1】:

您可以通过执行 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}/")

【讨论】:

    【解决方案2】:

    您可以直接发送页面,例如:

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

    所以

    当您运行程序时,有时它可以单击不同的按钮 ıdk 为什么当我制作相同的程序时会这样做:) 当您直接发送 hastags 的链接时,即使程序单击不同的按钮也会到达那里

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 2016-06-01
      • 2018-12-29
      • 2019-04-01
      • 2020-08-11
      • 2018-12-05
      相关资源
      最近更新 更多