如何使用Python和Selenium对Instagram帖子发表评论?
我有一个Instagram robot类,它接受用户名和密码并登录到该帐户。 我在这个类中编写了一个comment函数来将评论放在帖子下面,我使用这个函数,但是当它打开自定义帖子时,当我留下评论时,我会得到一个错误。 我不知道这个错误的原因 如果有人知道解决方案,请告诉我 我的代码:如何使用Python和Selenium对Instagram帖子发表评论?,python,selenium,selenium-webdriver,Python,Selenium,Selenium Webdriver,我有一个Instagram robot类,它接受用户名和密码并登录到该帐户。 我在这个类中编写了一个comment函数来将评论放在帖子下面,我使用这个函数,但是当它打开自定义帖子时,当我留下评论时,我会得到一个错误。 我不知道这个错误的原因 如果有人知道解决方案,请告诉我 我的代码: from selenium import webdriver import time import random class InstaBot: #Create a contractor to ope
from selenium import webdriver
import time
import random
class InstaBot:
#Create a contractor to open the browser and get the username and password of your Instagram account
def __init__(self,username,password):
self.driver = webdriver.Firefox()
self.username = username
self.password = password
#This function is used to login to your Instagram account
def login(self):
driver = self.driver
#Open the browser and open the Instagram site
driver.get('https://www.instagram.com')
#Find the username box address via XPath
user_box = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
#Find the password box address via XPath
password_box = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[2]/div/label/input')
#Find the login button via Xpath
button = driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[3]')
#Click on the username box
user_box.click()
#And enter the username in the username box
user_box.send_keys(self.username)
time.sleep(5)
#Click on the password box
password_box.click()
#And enter the password in the password box
password_box.send_keys(self.password)
time.sleep(5)
#Finally, click the Login button
button.click()
time.sleep(5)
#Go to the home page by adding a username to continue the Instagram link
acount = driver.get('https://www.instagram.com/%s/'%(self.username))
def comment(self,message=None):
driver = self.driver
driver.get('https://www.instagram.com/p/CLP-HN9AP0i/')
coment_box = driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/article/div[3]/section[3]/div/form/textarea').click()
coment_box.send_keys(message)
mobin = InstaBot('your user name','your password')
mobin.login()
time.sleep(5)
mobin.comment('Very Good')
它给我的错误是:
Traceback (most recent call last):
File "C:\Users\Mobin\Desktop\test_projcet.py", line 50, in <module>
mobin.comment('Very Good')
File "C:\Users\Mobin\Desktop\test_projcet.py", line 45, in comment
coment_box.send_keys(message)
AttributeError: 'NoneType' object has no attribute 'send_keys'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Mobin\Desktop\test\u projset.py”,第50行,在
mobin.comment(‘非常好’)
注释中第45行的文件“C:\Users\Mobin\Desktop\test\u projset.py”
输入框。发送密钥(消息)
AttributeError:“非类型”对象没有“发送密钥”属性
问题
问题在于,您将注释框设置为驱动程序操作而不是web元素。它将发出错误“对象没有属性‘send_keys’”,因为它不是web元素
此外,您的评论框被多次加载。因此,您需要在第一次单击后再次声明注释框
解决方案
删除此行末尾的单击
coment_box = driver.find_element_by_xpath('your xpath').click()
替换为此代码后,它应该可以工作:
coment_box = driver.find_element_by_xpath('your xpath')
coment_box.click()
time.sleep(5)
coment_box_2 = driver.find_element_by_xpath('your xpath')
coment_box.send_keys(message)
您的解决方案是添加一些隐式等待时间或exlicit等待时间,以确保驱动程序实际等待元素。在init中添加以下代码:self.driver.implicitly_wait(20)。