Selenium Python用于创建instagram登录喜欢机器人 [英] selenium python for creating a instagram login liker bot

查看:187
本文介绍了Selenium Python用于创建instagram登录喜欢机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用selenium for python为instagram创建一个简单的Liker机器人.想法是喜欢标记的第一张照片(在此示例中为日落").它会正确选择第一张照片,但不会插入喜欢的照片.

I am using selenium for python to create a simple liker bot for instagram. The idea is to like the first photo of a tag (in this example is "sunset"). It correctly selects the first photo but does not insert a like.

代码如下:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from time import sleep

import User_data

chrome_options=Options()

chrome_options.add_argument('--lang=en')
browser = webdriver.Chrome(chrome_options=chrome_options)

browser.get("https://www.instagram.com/accounts/login/")
sleep(1)

browser.find_element_by_name("username").send_keys(User_data.username)
browser.find_element_by_name("password").send_keys(User_data.password)

sleep(1)

browser.find_element_by_xpath('//*[@id="react-root"]/section/main/div/article/div/div[1]/div/form/div[3]/button').click()

sleep(1)
browser.get("https://www.instagram.com/explore/tags/sunset/")

sleep(1)
browser.find_element_by_xpath("//article/div[2]/div/div/div/a/div/div[2]").click()
sleep(1)
browser.find_element_by_xpath("//button/span[contains(@class. 'glyphsSpriteHeart__outline__24__grey_9 u-__7') ]").click()

推荐答案

之所以无法登录,是因为您的代码无法找到您指定的element by xpath.这可能是因为不支持您提供的xpath.

The reason you're unable to Log In is because your code is unable to find the element by xpath you specified. That is probably because the xpath that you provide is not supported.

我发现了一种解决方法,使用函数find_elements_by_tag_name()返回按钮列表,通过简单的迭代,我发现返回的第二个按钮与Instagram上的登录"按钮相对应.因此,这是一个有用的代码,您会发现它很有用.

I have found a workaround for this where I use the function find_elements_by_tag_name() which returns me the list of buttons, and with simple iteration I found that the second button returned corresponds to the Log In button on Instagram. So here's a working code that you will find useful.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from time import sleep

import User_data

chrome_options=Options()

chrome_options.add_argument('--lang=en')
browser = webdriver.Chrome(chrome_options=chrome_options)

browser.get("https://www.instagram.com/accounts/login/")
sleep(1)

browser.find_element_by_name("username").send_keys(User_data.username)
browser.find_element_by_name("password").send_keys(User_data.password)

sleep(2)

buttons = browser.find_elements_by_tag_name('button')
for button_element in buttons:
    print(button_element.text)     #- This will print the text of the buttons present

这将为您打印所有按钮的text字段: 输出:

This will print you the text fields of all the buttons: Output:

Show
Log in
Log in with Facebook

这是我的输出中出现的三个按钮,由于我必须单击文本中带有Log in的按钮(这是列表buttons[]中的第二个元素),因此我使用索引1来表示然后单击它.

These are the three buttons that come in my output, and since I have to click on the button with Log in in it's text, which is the second element from my list buttons[], I use index 1 to denote that and click it.

buttons[1].click()

这篇关于Selenium Python用于创建instagram登录喜欢机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆