如何迭代并单击Instagram帖子Web元素列表-Selenium
因此,目前,我正在尝试构建一个在Instagram上搜索特定主题标签的机器人,找到使用该主题标签发表帖子的每个用户,并向他们发送消息。
我能够从特定DIV中收集一个代表“标签”页面上的每个帖子的Web元素列表。现在,我的代码将遍历每个元素,试图单击页面上的每个帖子。
但是,为我点击的努力是如何在单击一篇文章后进入下一篇文章。
我当前的逻辑几乎只会遍历列表,然后重新单击该页面上的第一篇文章。以下是一些代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time, urllib.request
import requests
from dotenv import load_dotenv
import sys
import os
# get prod.env vars
sys.path.insert(0, os.environ['HOME'])
dotenv_path = os.environ['HOME'] + '/prod.env'
load_dotenv(dotenv_path, override=True)
locals().update(os.environ)
# Search up hashtags
PATH = "/Users/Nick/Desktop/chromedriver"
# headless options
# op = webdriver.ChromeOptions()
# op.add_argument('headless')
driver = webdriver.Chrome(executable_path=PATH)
ig = driver.get("https://www.instagram.com/")
time.sleep(5)
# username and password input
username=driver.find_element_by_css_selector("input[name='username']")
password=driver.find_element_by_css_selector("input[name='password']")
username.clear()
password.clear()
username.send_keys(ig_user)
password.send_keys(ig_pass)
driver.find_element_by_css_selector("button[type='submit']").click()
time.sleep(10)
# take care of popups
notnow = driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
time.sleep(10)
notnow2 = driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
time.sleep(5)
# # search hashtag
query = "goodproblems"
page = driver.get("https://www.instagram.com/explore/tags/" + query)
time.sleep(5)
# driver.find_element_by_class_name('_9AhH0')
post_divs = [elem for elem in driver.find_elements_by_class_name('_9AhH0')]
for post in post_divs:
time.sleep(2)
the_post = driver.find_element_by_xpath("//*[@id='react-root']/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]").click()
time.sleep(4)
name = driver.find_element_by_xpath("/html/body/div[6]/div[3]/div/article/div/div[2]/div/div/div[2]/div[1]/ul/div/li/div/div/div[2]/h2/div/span/a").click()
time.sleep(3)
try:
message = driver.find_element_by_css_selector("button[type='button']").click()
time.sleep(2)
driver.back()
# cancel = driver.find_element_by_xpath("/html/body/div[6]/div/div/div/div/button[5]").click()
driver.back()
driver.back()
time.sleep(4)
# message_input= driver.find_element_by_xpath("//*[@id='react-root']/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea").send_keys("Sample message")
except:
print("no message button")
# cancel = driver.find_element_by_xpath("/html/body/div[6]/div/div/div/div/button[5]").click()
driver.back()
driver.back()
driver.back()
# cancel = driver.find_element_by_xpath("/html/body/div[6]/div/div/div/div/button[5]").click()
time.sleep(4)
So right now, I'm trying to build a bot that searches a particular hashtag on instagram, finds each user who made a post using that hashtag, and sends them a message.
I was able to collect a list of web elements from a particular div that represents every post on a "hash-tagged" page. And right now, my code will iterate through each element, trying to click on each post on the page.
However, what's struggling to click for me is how to move onto the next post after one post has been clicked on.
Pretty much, my current logic will just loop through the list, and repeadetly click on the first post on that page. Here's some code below:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time, urllib.request
import requests
from dotenv import load_dotenv
import sys
import os
# get prod.env vars
sys.path.insert(0, os.environ['HOME'])
dotenv_path = os.environ['HOME'] + '/prod.env'
load_dotenv(dotenv_path, override=True)
locals().update(os.environ)
# Search up hashtags
PATH = "/Users/Nick/Desktop/chromedriver"
# headless options
# op = webdriver.ChromeOptions()
# op.add_argument('headless')
driver = webdriver.Chrome(executable_path=PATH)
ig = driver.get("https://www.instagram.com/")
time.sleep(5)
# username and password input
username=driver.find_element_by_css_selector("input[name='username']")
password=driver.find_element_by_css_selector("input[name='password']")
username.clear()
password.clear()
username.send_keys(ig_user)
password.send_keys(ig_pass)
driver.find_element_by_css_selector("button[type='submit']").click()
time.sleep(10)
# take care of popups
notnow = driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
time.sleep(10)
notnow2 = driver.find_element_by_xpath("//button[contains(text(), 'Not Now')]").click()
time.sleep(5)
# # search hashtag
query = "goodproblems"
page = driver.get("https://www.instagram.com/explore/tags/" + query)
time.sleep(5)
# driver.find_element_by_class_name('_9AhH0')
post_divs = [elem for elem in driver.find_elements_by_class_name('_9AhH0')]
for post in post_divs:
time.sleep(2)
the_post = driver.find_element_by_xpath("//*[@id='react-root']/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]").click()
time.sleep(4)
name = driver.find_element_by_xpath("/html/body/div[6]/div[3]/div/article/div/div[2]/div/div/div[2]/div[1]/ul/div/li/div/div/div[2]/h2/div/span/a").click()
time.sleep(3)
try:
message = driver.find_element_by_css_selector("button[type='button']").click()
time.sleep(2)
driver.back()
# cancel = driver.find_element_by_xpath("/html/body/div[6]/div/div/div/div/button[5]").click()
driver.back()
driver.back()
time.sleep(4)
# message_input= driver.find_element_by_xpath("//*[@id='react-root']/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea").send_keys("Sample message")
except:
print("no message button")
# cancel = driver.find_element_by_xpath("/html/body/div[6]/div/div/div/div/button[5]").click()
driver.back()
driver.back()
driver.back()
# cancel = driver.find_element_by_xpath("/html/body/div[6]/div/div/div/div/button[5]").click()
time.sleep(4)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论