如何解决Instagram上的“上传”按钮不接受Selenium send_keys
我正在编码一个Selenium机器人(使用Python),该机器人从目录中的队列将图片上传到instagram。目前,我已经成功登录了Instagram,现在我正尝试与上传按钮进行某种交互。
我尝试在其上单击(),但是随后弹出一个窗口,在该窗口中,我通常会浏览计算机以查找要上传的图像。我发现我需要import autoit
,但是我不知道它是如何工作的,文档也没有帮助,所以我宁愿避免使用它。
这是我现在拥有的:
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import webdriverwait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
class InstaBot():
# A COOKIES POP UP ALWAYS APPEARS UPON opening INSTAGRAM,SO INIT ALSO CLOSES IT
# TO UPLOAD ON INSTAGRAM,THE MOBILE VERSION IS NEEDED,WE TRY TO EMULATE A galAXY S5
def __init__(self):
mobile_emulation = {"deviceName": "galaxy S5"}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option("mobileEmulation",mobile_emulation)
# experimental options for mobile emulation added
self.driver = webdriver.Chrome(ChromeDriverManager().install(),chrome_options = chrome_options)
self.driver.get("https://www.instagram.com/")
# note that this accepts all cookies
cooki = self.driver.find_element_by_xpath('/html/body/div[2]/div/div/div/div[2]/button[1]')
cooki.click()
# FINDS THE USERNAME AND PASSWORD AND TYPES 2 INPUTS ACCORDINGLY
def loginfun(self):
entrar = webdriverwait(self.driver,10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="react-root"]/section/main/article/div/div/div/div[2]/button')))
entrar.click()
usbar = pasbar = self.driver.find_element_by_xpath('//*[@id="loginForm"]/div[1]/div[3]/div/label/input')
usbar.send_keys(input('Username: '))
# once usbar is found,the rest will be as well
pasbar = self.driver.find_element_by_xpath('//*[@id="loginForm"]/div[1]/div[4]/div/label/input')
pasbar.send_keys(input('Password: '))
logbtn = self.driver.find_element_by_xpath('//*[@id="loginForm"]/div[1]/div[6]/button')
logbtn.click()
# CLOSES PASSWORD SAVING AND NOTIFICATION MESSAGES IN CASE They APPEAR
def tryclose(self):
try:
nopass = webdriverwait(self.driver,'//*[@id="react-root"]/section/main/div/div/div/button')))
nopass.click()
except Exception:
pass
try:
nonot = webdriverwait(self.driver,'/html/body/div[4]/div/div/div/div[3]/button[2]')))
nonot.click()
except Exception:
pass
# bla bla bla
# Basically I emulate a mobile device and log into my account,# then I have at the bottom center of the page the upload button that
# looks like this [+]. Here is what I try:
# SELECTS THE FirsT IMAGE FROM THE PENDING DIR. USES THE UPLOAD BUTTON VIA SEND_KEYS.
# AFTER THAT,IT MOVES SAID IMAGE TO THE 'DONE' DIR.
def upload(self):
# first pending image
pend_img = (os.listdir('C:/path to my queue dir')[0])
# finds the upload button and send_keys the image to it
upbtn = self.driver.find_element_by_xpath('//*[@id="react-root"]/section/nav[2]/div/div/form/input')
upbtn.send_keys('C:/path to image in queue'+pend_img)
# moves the image to the 'done' directory
os.rename('C:/path to image in queue dir'+pend_img,\
'C:/path to image in done dir'+pend_img)
在此过程之后,此代码能够在“待处理”(队列)目录中找到图像并将其移至“完成”目录,但它不会与instagram进行任何交互。因此send_keys()
无法正常工作。我是新手,但我确认按钮upbtn
的HTML路径可能是错误的,尽管我找不到任何其他输入路径或任何东西。
注意:为澄清起见,未显示任何错误,问题仅在于send_keys不会与带有此代码的上传按钮交互。
任何人都有一个简单的解决方案或直观的方式可以上传到Instagram吗?
搜索之后,这些都是我可以找到的输入标签的所有XPath,它们都有type="file"
:
//*[@id="react-root"]/form/input
//*[@id="react-root"]/section/main/div[1]/form/input
//*[@id="react-root"]/section/nav[1]/div/div/form/input # this one is for stories i think
//*[@id="react-root"]/section/nav[2]/div/div/form/input # it should be this one
我已经尝试对所有send_keys()
进行了操作,但似乎没有一个起作用。
解决方法
如果您不介意的话,我推荐一个可能有用并且效果很好的软件包!
Instabot能够上传模拟移动设备的照片/视频和故事。
安装:
pip install instabot
实施:
#Call bot
from instabot import Bot
然后只需要几个步骤:
bot = Bot()
bot.login(username = 'user',password = 'pass')
bot.upload_photo(image_path,caption = 'Hello world')
我建议使用此选项,因为它干净,快速且可靠
更多信息,请访问:https://pypi.org/project/instabot/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。