使用 Selenium 和 python 为 Instagram 提供上传文件路径
Give upload file path to Instagram with Selenium and python
我正在使用 Selenium 和 Python 在 Instagram 上测试一些网络抓取。
在这种情况下,我想上传一张图片。
通常您必须单击上传图标并从 window 中选择文件。我如何使用 Selenium 来管理它?
我试过了:
driver.find_element_by_class_name("coreSpriteFeedCreation").send_keys('C:\path-to-file\file.jpg')
还有 find_element_by_xpath
但我得到一个例外:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
我也只尝试了 click()
但没有任何反应。
有什么想法吗?
编辑
感谢@homersimpson 的评论,我试过了:
actions = ActionChains(driver)
element = driver.find_element_by_class_name("coreSpriteFeedCreation")
actions.move_to_element(element)
actions.click()
actions.send_keys('C:\path-to-file\file.jpg')
actions.perform()
现在 window 选择文件出现。问题是我想避免这个 window 并直接给出我的文件的路径。
如果理解正确,您是在尝试避免使用本地人处理 window。你可以试试这个:
# get all inputs
inputs = driver.find_elements_by_xpath("//input[@accept = 'image/jpeg']").send_keys(os.getcwd() + "/image.png")
现在您可以尝试所有这些。不知道哪个好用
关于 os.getcwd()
的更多信息是 here
为了能够执行此代码,您必须具有如下元素:
<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">
编辑:
看起来 instagram
关闭了 post 秒的输入字段交互。对于帐户图像,它仍然有效,但不适用于 posting。我认为这样做是为了防止机器人使用 post 图片。不管怎样,这个问题是有解决办法的。您可以像这样使用 AutoIt:
import autoit
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
ActionChains(driver).move_to_element( driver.find_element_by_xpath("//path/to/upload/button")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 60)
autoit.control_set_text(handle, "Edit1", "\file\path")
autoit.control_click(handle, "Button1")
我想我可能找到了适合我的解决方案。我发现如果您首先让机器人在浏览器处于移动视图时单击加号图标。
self.driver.find_element_by_xpath("/html/body/div[1]/section/nav[2]/div/div/div[2]/div/div/div[3]")\
.click()
之后,我会立即将我的文件发送到 HTML 中的一个输入标签,我发现您可能需要试一试哪个有效,但我发现最后一个输入标签对我有效。
self.driver.find_element_by_xpath("/html/body/div[1]/section/nav[2]/div/div/form/input")\
.send_keys("/image/path.jpg")
关于此的一件奇怪的事情是,您将在页面顶部有一个弹出菜单,但您的代码仍然可以运行 window,显示在您正在处理的 window 上。
添加到 HumbleFox 的回答。解决他弹出框不关闭或文件弹出框不关闭的问题(bug)
解决这个问题的方法是让浏览器无头这里是我的代码的一部分例如:
mobile_emulation = { "deviceName": "Pixel 2" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
chrome_options.binary_location = self.opt.binary_location
self.driver = webdriver.Chrome(executable_path=self.path, options=chrome_options)
我正在使用 Selenium 和 Python 在 Instagram 上测试一些网络抓取。
在这种情况下,我想上传一张图片。
通常您必须单击上传图标并从 window 中选择文件。我如何使用 Selenium 来管理它?
我试过了:
driver.find_element_by_class_name("coreSpriteFeedCreation").send_keys('C:\path-to-file\file.jpg')
还有 find_element_by_xpath
但我得到一个例外:
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element
我也只尝试了 click()
但没有任何反应。
有什么想法吗?
编辑
感谢@homersimpson 的评论,我试过了:
actions = ActionChains(driver)
element = driver.find_element_by_class_name("coreSpriteFeedCreation")
actions.move_to_element(element)
actions.click()
actions.send_keys('C:\path-to-file\file.jpg')
actions.perform()
现在 window 选择文件出现。问题是我想避免这个 window 并直接给出我的文件的路径。
如果理解正确,您是在尝试避免使用本地人处理 window。你可以试试这个:
# get all inputs
inputs = driver.find_elements_by_xpath("//input[@accept = 'image/jpeg']").send_keys(os.getcwd() + "/image.png")
现在您可以尝试所有这些。不知道哪个好用
关于 os.getcwd()
的更多信息是 here
为了能够执行此代码,您必须具有如下元素:
<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">
编辑:
看起来 instagram
关闭了 post 秒的输入字段交互。对于帐户图像,它仍然有效,但不适用于 posting。我认为这样做是为了防止机器人使用 post 图片。不管怎样,这个问题是有解决办法的。您可以像这样使用 AutoIt:
import autoit
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
ActionChains(driver).move_to_element( driver.find_element_by_xpath("//path/to/upload/button")).click().perform()
handle = "[CLASS:#32770; TITLE:Open]"
autoit.win_wait(handle, 60)
autoit.control_set_text(handle, "Edit1", "\file\path")
autoit.control_click(handle, "Button1")
我想我可能找到了适合我的解决方案。我发现如果您首先让机器人在浏览器处于移动视图时单击加号图标。
self.driver.find_element_by_xpath("/html/body/div[1]/section/nav[2]/div/div/div[2]/div/div/div[3]")\
.click()
之后,我会立即将我的文件发送到 HTML 中的一个输入标签,我发现您可能需要试一试哪个有效,但我发现最后一个输入标签对我有效。
self.driver.find_element_by_xpath("/html/body/div[1]/section/nav[2]/div/div/form/input")\
.send_keys("/image/path.jpg")
关于此的一件奇怪的事情是,您将在页面顶部有一个弹出菜单,但您的代码仍然可以运行 window,显示在您正在处理的 window 上。
添加到 HumbleFox 的回答。解决他弹出框不关闭或文件弹出框不关闭的问题(bug)
解决这个问题的方法是让浏览器无头这里是我的代码的一部分例如:
mobile_emulation = { "deviceName": "Pixel 2" }
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
chrome_options.binary_location = self.opt.binary_location
self.driver = webdriver.Chrome(executable_path=self.path, options=chrome_options)