繁体   English   中英

使用 Selenium 制作 Instagram Bot

[英]Using Selenium to make Instagram Bot

我正在尝试在 Python 中制作一个机器人,它可以直接向我选择的用户发送消息。 我能够对所有内容进行编码,直到在个人资料上选择“消息”。

部分有效的代码:

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)

此代码将我一直带到用户页面。 但是,当我尝试“单击”消息按钮时,找不到 xpath。 这是代码:

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

我通过右键单击消息按钮 > 检查 > 突出显示右侧的 HTML 代码 > 复制 > 复制 XPath 找到了 xpath。

我也尝试过完整的 xpath,但似乎也找不到。

这是我所指的按钮的屏幕截图:

在此处输入图像描述

我复制了你的代码,并试图做出你想要的。 我设法使 selenium go 成为您要向其发送消息的用户的消息。 这是代码:

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()

我将所有 sleep() 函数替换为等待加载元素的行。 我认为您的问题是您的脚本在加载之前尝试定位元素。

在点击 « 消息 » 按钮之前,您应该输入以下行:

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’)))

并导入这个:

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

它将等到按钮加载完毕,然后单击它。

如果还是不行,你可以试试其他方法点击按钮,比如css_selector,class_name Id...

暂无
暂无

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

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