你好,因為 Instagram API 不再工作了,但我仍然想要一個 Instagram 機器人,我開始了自己的小項目。一切都很順利,直到我遇到了一件非常有趣的事情。當我單擊上傳圖片按鈕時,會出現文件對話框,這也是計劃好的,然后我想做的是模擬按鍵,所以沒什么特別的(也在我的腳本中稍早做了一點)但是出于什么原因我的python當涉及到文件對話框時,腳本只是停止,沒有錯誤或其他任何內容,腳本也仍然處于活動狀態,只是不想轉到下一行。from selenium import webdriverfrom time import sleepimport keyboardfrom selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome()driver.get("https://instagram.com")sleep(2)#login into Accountusername = 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 hereusername.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 partuploadPictureBtn = 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')
1 回答

慕標5832272
TA貢獻1966條經驗 獲得超4個贊
該頁面“坐立”是因為 selenium 無法處理上傳窗口,為此,您需要使用 AutoIt(如果您在 Windows 上)或 Robot Framework 來處理該問題。過去它也可以在輸入文件上使用 send_keys 方法。
添加回答
舉報
0/150
提交
取消