Sha*_*eek 1 python upload selenium json
背景:
我一直试图解决这个问题一段时间了。我正在使用 Selenium (chrome webdriver) 从桌面访问 Instagram 的移动版本。我的目标是创建一个脚本来自动将图像发布到帐户。使用 Python,我已经自动化了一切,直到我需要选择文件的部分。
问题:
单击创建新帖子按钮后,gui 文件选择器将打开,我无法弄清楚如何与其交互以选择文件。
尝试:
post_btn.send_keys(r'/Path/To/image.jpg')
post_btn.send_keys('/Path/To/image.jpg')
我认为,这种方法的麻烦在于没有将字符串发送到的输入字段。
我阅读了其他一些关于使用 autoit 和工作的帖子,但我在 Mac 上。
这是帖子按钮所在的跨度:
<span class="glyphsSpriteNew_post__outline__24__grey_9 u-__7" aria-label="New Post"></span>
Run Code Online (Sandbox Code Playgroud)
题:
有没有办法使用python通过文件选择器gui“打开”(上传)文件?更好的是,我可以绕过打开文件选择器吗?
谢谢!
编辑:
我认为阻止我解决这个问题的是我不明白数据是如何发送和接收的。既然它不是 html 输入,这是否意味着它是 JSON?我如何弄清楚数据(图像)如何发送到下一页(https://www.instagram.com/create/style/)?
小智 5
IDK 如果这对你有用,因为它已经很久了。但是对于任何需要它的人来说,这里是完整的教程,
进口:
import os
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import autoit
import time
from selenium.webdriver.common.keys import Keys
Run Code Online (Sandbox Code Playgroud)
所以首先你需要一个模拟器。我发现的最佳方法是使用 chromedriver.exe,如果您使用 chrome 作为浏览器,则您已经需要它。因此,要制作模拟器,您需要为驱动程序编写以下代码:
mobile_emulation = {
"deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
"userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options = chrome_options)
Run Code Online (Sandbox Code Playgroud)
然后你登录:
driver.get('https://www.instagram.com/accounts/login/')
time.sleep(2)
driver.find_element_by_name("username").send_keys("Your Username")
driver.find_element_by_name("password").send_keys("Your Password")
driver.find_element_by_xpath("""//*[@id="react-root"]/section/main/article/div/div/div/form/div[7]/button""").click()
time.sleep(2)
driver.get('https://www.instagram.com/' + username)
Run Code Online (Sandbox Code Playgroud)
命名您的文件(必须是全名):
ImagePath = 'Your File Location'
Run Code Online (Sandbox Code Playgroud)
最后一步是这样的:
ActionChains(driver).move_to_element( driver.find_element_by_xpath("""//*[@id="react-root"]/section/nav[2]/div/div/div[2]/div/div/div[3]""")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 3)
autoit.control_set_text(handle, "Edit1", dir_path)
autoit.control_click(handle, "Button1")
time.sleep(2)
driver.find_element_by_xpath("""//*[@id="react-root"]/section/div[1]/header/div/div[2]/button""").click()
time.sleep(2)
txt = driver.find_element_by_class_name('_472V_')
txt.send_keys('')
txt = driver.find_element_by_class_name('_472V_')
txt.send_keys('test') # Descrition
txt.send_keys(Keys.ENTER)
driver.find_element_by_xpath("""//*[@id="react-root"]/section/div[1]/header/div/div[2]/button""").click()
Run Code Online (Sandbox Code Playgroud)
所有这些都是转到上传页面并使用 autoit 浏览窗口以选择文件并进行选择。然后只需添加描述并分享帖子。
| 归档时间: |
|
| 查看次数: |
4816 次 |
| 最近记录: |