Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Selenium和python为Instagram提供上传文件路径_Python_Selenium_Selenium Webdriver_Web Scraping_Instagram - Fatal编程技术网

使用Selenium和python为Instagram提供上传文件路径

使用Selenium和python为Instagram提供上传文件路径,python,selenium,selenium-webdriver,web-scraping,instagram,Python,Selenium,Selenium Webdriver,Web Scraping,Instagram,我正在用Selenium和Python在Instagram上测试一些网页抓取 在这种情况下,我想上传一张照片 通常,您必须单击上载图标并从窗口中选择文件。我如何使用硒来管理它 我试过: driver.find_element_by_class_name("coreSpriteFeedCreation").send_keys('C:\\path-to-file\\file.jpg') 还有find_element\u by_xpath,但我得到一个例外: selenium.common.exce

我正在用Selenium和Python在Instagram上测试一些网页抓取

在这种情况下,我想上传一张照片

通常,您必须单击上载图标并从窗口中选择文件。我如何使用硒来管理它

我试过:

driver.find_element_by_class_name("coreSpriteFeedCreation").send_keys('C:\\path-to-file\\file.jpg')
还有
find_element\u 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()

现在,选择文件的窗口出现。问题是,我希望避免使用此窗口,并直接给出文件的路径。

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

# get all inputs
inputs = driver.find_elements_by_xpath("//input[@accept = 'image/jpeg']").send_keys(os.getcwd() + "/image.png")
现在你可以全部试试。我不知道他们中的哪一个会起作用

有关
os.getcwd()
的更多信息

为了能够执行此代码,您必须具有如下元素:

<input type="file" name="fileToUpload" id="fileToUpload2" class="fileToUpload">
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")

关于这一点,有一件奇怪的事情是,你会在页面顶部有一个弹出菜单,但是你的代码仍然会在你正在工作的窗口上显示这个窗口。

除了HumbleFox的答案之外。解决弹出框未关闭或文件弹出框未关闭的问题(错误)

解决方法是使浏览器无头,这是我代码的一部分,例如:

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)

这里可能有一个答案:谢谢@homersimpson,我编辑了我的问题你能显示文件上传窗口的HTML片段吗?我总是得到
selenium.common.exceptions.WebDriverException:消息:未知错误:无法聚焦元素
很高兴听到它,今天我已经达到了向下/向上投票的限制,所以明天我会投票表决你的问题。你有一个很好的方法)@AndreiSuvorkov:我会为你做的。小问题,它工作得很好,但是它打开了错误的输入。使用你的代码,我可以更改我的个人资料图片。我尝试了DOM中的所有输入,但没有成功。有什么想法吗?我已经更改了我的
xPath
。因此,我根据新的
xPath
找到了三个元素。我不知道他们中的哪一个是对的,但他们中的一个应该做到这一点。请尝试告诉我它是否有效,以及哪个元素是正确的。