簡體   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