Python 按下网络按钮时脚本停止工作

Python script stops working when web button gets pressed

你好,因为 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' 因为 selenium 无法处理上传 windows,为此您需要使用 AutoIt(如果您使用的是 windows)或 Robot Framework 来处理.过去,它还对输入文件使用 send_keys 方法。

Here is a topic on that