使用Selenium和python将上传文件路径提供给Instagram [英] Give upload file path to Instagram with Selenium and python

查看:312
本文介绍了使用Selenium和python将上传文件路径提供给Instagram的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium和Python在Instagram上测试一些Web抓取.

I'm testing some web scraping on Instagram with Selenium and Python.

在这种情况下,我要上传图片.

In this case I want to upload a picture.

通常,您必须单击上载图标,然后从窗口中选择文件.如何使用Selenium进行管理?

Normally you have to click on the upload icon and choose the file from a window. How can I manage it with 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()尝试过,但是什么也没发生.

I tried also only with click() but nothing happens.

有什么想法吗?

编辑
感谢@homersimpson评论,我尝试了这一点:

EDIT
Thanks to @homersimpson comment I tried this:

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()

现在将显示选择文件的窗口.问题是我想避开这个窗口,直接给出文件的路径.

Now the window to choose the file appears. The problem is that I would like to avoid this window and give directly the path of my file.

推荐答案

如果正确理解,您将尝试避免使用本机窗口进行处理.您可以尝试以下方法:

If right understand, you are trying to avoid handling with a native window. You can try this:

# get all inputs
inputs = driver.find_elements_by_xpath("//input[@accept = 'image/jpeg']").send_keys(os.getcwd() + "/image.png")

现在您可以尝试所有这些.我不知道他们中的哪个会起作用.

Now you can try all of them. I don't know which of them will work.

关于os.getcwd()的更多信息是此处

要执行此代码,您必须具有以下元素:

To be able to perform this code you have to have an element like this:

<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">

看起来像instagram的帖子输入字段交互一样.对于帐户"图片,它仍然有效,但不适用于过帐.我认为这样做是为了防止漫游器发布图像.无论如何,有解决此问题的方法.您可以这样使用AutoIt:

It looks like instagram turned of input fields interaction for posts. For Account image it still works, but not for posting. I assume it is was done to prevent bots to post images. Anyway, there is a solution for this problem. You can use AutoIt like this:

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")

这篇关于使用Selenium和python将上传文件路径提供给Instagram的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆