按下Web按钮时Python脚本停止工作

问题描述

您好,因为instagram API不再起作用,但是我仍然想要instagram机器人,所以我开始了自己的小项目。一切都会好起来,直到我遇到一件非常有趣的事情。

当我单击“上传图片”按钮时,也会显示文件对话框,这也是经过计划的,然后我想做的是模拟按键,所以没什么特别的(在我的脚本中也做了些)我的python脚本在出现文件对话框时只是停止的原因,没有错误或其他任何原因,该脚本也仍然处于活动状态,只是不想转到下一行。

from selenium import webdriver
from time import sleep
import keyboard
from selenium.webdriver.common.action_chains import ActionChains 

driver = webdriver.Chrome()
driver.get("https://instagram.com")

sleep(2)
#login into Account
username = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input")
password = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[2]/div/label/input")
loginBtn = driver.find_element_by_xpath("/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[3]/button")

#put your username and password here
username.send_keys("username")
password.send_keys("pw")
loginBtn.click()
sleep(4)

#----------------------------------

safeLoginDateBTN = driver.find_element_by_xpath("/html/body/div[1]/section/main/div/div/div/div/button")
safeLoginDateBTN.click()
sleep(4)
notNowNotifications = driver.find_element_by_xpath("/html/body/div[4]/div/div/div/div[3]/button[2]")
notNowNotifications.click()
sleep(4)

#Now comes the interestig part
#The problem is that there is no instgram API anymore so what are we going to do?
#exactly we are going to say instagram that we are actually a smarthphone so the browser switches to "phone mode"

keyboard.press_and_release('F12')
sleep(2)
keyboard.press_and_release('Cmd+Shift+M')
sleep(2)
driver.refresh()

#Now we are in the device mode
#this is a little workaround because the sh*ti instagram api isnt working anymore
#Now comes the upload part

uploadPictureBtn = driver.find_element_by_xpath("/html/body/div[1]/section/nav[2]/div/div/div[2]/div/div/div[3]")
uploadPictureBtn.click()


keyboard.press_and_release('PicsForInstagram')
keyboard.press_and_release('Enter')

解决方法

页面“ sits”,因为硒无法处理上载窗口,为此,您需要使用AutoIt(如果您在Windows上)或Robot Framework来处理。过去,它还可以在输入文件中使用send_keys方法。

enter image description here