简体   繁体   English

使用 Selenium 制作 Instagram Bot

[英]Using Selenium to make Instagram Bot

I'm trying to make a bot in Python that directly messages a user of my choice.我正在尝试在 Python 中制作一个机器人,它可以直接向我选择的用户发送消息。 I was able to code everything up and until selecting 'Message' on the persons profile.我能够对所有内容进行编码,直到在个人资料上选择“消息”。

Part of the code that works:部分有效的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint


chromedriver_path = 'C:/Users/JACOB/Downloads/chromedriver_win32 (1)/chromedriver.exe' 
webdriver = webdriver.Chrome(executable_path=chromedriver_path)
sleep(2)
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
sleep(3)

username = webdriver.find_element_by_name('username')
username.send_keys('MYUSERNAME')
password = webdriver.find_element_by_name('password')
password.send_keys('MYPASSWORD')

button_login = webdriver.find_element_by_css_selector('#react-root > section > main > div > article > div > div > div > form > div > button.sqdOP.L3NKy.y3zKF')
button_login.click()

sleep(4)

notnow = webdriver.find_element_by_css_selector('#react-root > section > main > div > div > div > div > button.sqdOP.yWX7d.y3zKF')
notnow.click()

sleep (4)

postnotifications = webdriver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[3]/button[2]')
postnotifications.click()

sleep(3)

mydms = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[3]/div/div[2]/a/svg')
mydms.click()

search = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input')
search.send_keys('INSTAGRAM_USERNAME')

sleep(2)

foundit = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a/div/div[2]/div/span')
foundit.click()

sleep(3)

This code takes me all the way to a users page.此代码将我一直带到用户页面。 However when I attempt to 'click' on the Message button, the xpath can't be found.但是,当我尝试“单击”消息按钮时,找不到 xpath。 Here is the code:这是代码:

message = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[2]/div[1]/div/button')
message.click()

I found the xpath by right clicking on the Message button > Inspect > Highlighting the HTML code on the right hand side > Copy > Copy XPath.我通过右键单击消息按钮 > 检查 > 突出显示右侧的 HTML 代码 > 复制 > 复制 XPath 找到了 xpath。

I've also tried the full xpath and it can't seem to find it either.我也尝试过完整的 xpath,但似乎也找不到。

Here is a screenshot of the button I'm referring too:这是我所指的按钮的屏幕截图:

在此处输入图像描述

I copied your code, and tried to make what you want.我复制了你的代码,并试图做出你想要的。 I manage to make selenium go to the messages of the user you want to send the message to.我设法使 selenium go 成为您要向其发送消息的用户的消息。 Here is the code:这是代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep, strftime
from random import randint
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

chromedriver_path = 'C:/Users/JACOB/Downloads/chromedriver_win32 (1)/chromedriver.exe'
webdriver = webdriver.Chrome()
webdriver.maximize_window()
webdriver.get('https://www.instagram.com/accounts/login/?source=auth_switcher')
WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.NAME, 'username')))

username = webdriver.find_element_by_name('username')
username.send_keys('username')
password = webdriver.find_element_by_name('password')
password.send_keys('password')

button_login = webdriver.find_element_by_css_selector('#react-root > section > main > div > article > div > div > div > form > div > button.sqdOP.L3NKy.y3zKF')
button_login.click()

WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#react-root > section > main > div > div > div > div > button.sqdOP.yWX7d.y3zKF')))

notnow = webdriver.find_element_by_css_selector('#react-root > section > main > div > div > div > div > button.sqdOP.yWX7d.y3zKF')
notnow.click()

WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[4]/div/div/div/div[3]/button[2]')))

postnotifications = webdriver.find_element_by_xpath('/html/body/div[4]/div/div/div/div[3]/button[2]')
postnotifications.click()

WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/section/nav/div[2]/div/div/div[3]/div/div[3]/a')))


WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input')))

search = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/input')
sleep(2)
search.send_keys('Username')

WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a/div/div[2]/div/span')))

foundit = webdriver.find_element_by_xpath('//*[@id="react-root"]/section/nav/div[2]/div/div/div[2]/div[2]/div[2]/div/a/div/div[2]/div/span')
foundit.click()

WebDriverWait(webdriver, 20).until(EC.presence_of_element_located((By.XPATH, '//*[@id="react-root"]/section/main/div/header/section/div[1]/div[1]/div/button')))

webdriver.find_element_by_xpath('//*[@id="react-root"]/section/main/div/header/section/div[1]/div[1]/div/button').click()

I replace all the sleep() functions with the line to wait that the element is loaded.我将所有 sleep() 函数替换为等待加载元素的行。 I think that your problem was that your script tried to locate the element before it was loaded.我认为您的问题是您的脚本在加载之前尝试定位元素。

Before clicking the « message » button, you should put this line:在点击 « 消息 » 按钮之前,您应该输入以下行:

WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, ‘//*[@id="react-root"]/section/main/div/header/section/div[2]/div[1]/div/button’)))

And import this:并导入这个:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

It will wait until the button is loaded and then click it.它将等到按钮加载完毕,然后单击它。

If it is still not working, you can just try other methods to click the button, like css_selector, class_name Id...如果还是不行,你可以试试其他方法点击按钮,比如css_selector,class_name Id...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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