在 Python 中使用 Selenium 上传文件

发布于 2024-12-20 04:42:54 字数 38 浏览 5 评论 0原文

是否可以在Python脚本中使用selenium上传文件附件?

Is it possible to upload file attachment with selenium in Python script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

Saygoodbye 2024-12-27 04:42:54

可以通过以下方式完成:

element = driver.find_element_by_name("file")
element.send_keys("/home/pavel/Desktop/949IH3GNHAo.jpg")

It can be done via:

element = driver.find_element_by_name("file")
element.send_keys("/home/pavel/Desktop/949IH3GNHAo.jpg")
溺渁∝ 2024-12-27 04:42:54

上传文件的一个简单方法是使用 pyautogui。可以通过pip安装pyautogui

import pyautogui
... # set the webdriver etc.
...
...

self.driver.find_element_by_id("Open file selector").click()# This opens the windows file selector
pyautogui.write('C:/path_to_file') 
pyautogui.press('enter')

A simple method to upload files is by using pyautogui. You can install pyautogui through pip

import pyautogui
... # set the webdriver etc.
...
...

self.driver.find_element_by_id("Open file selector").click()# This opens the windows file selector
pyautogui.write('C:/path_to_file') 
pyautogui.press('enter')
青朷 2024-12-27 04:42:54
button = driver.find_element_by_xpath("xpathToYourButton")
button.send_keys("fullPathToFile")

现在,如果您在 Windows 中,文件路径使用反斜杠。为了避免出现问题,使用双反斜杠! C:\ \Users\ ****\ \Desktop\ \1.jpg,不带空格。

附言。我知道它是 4 年前的,但我已经尝试弄清楚这一点有一段时间了,有人可能会发现这很有用......

button = driver.find_element_by_xpath("xpathToYourButton")
button.send_keys("fullPathToFile")

Now if you are in windows path to file uses backslash. To avoid issues use double backslashes! C:\ \Users\ ****\ \Desktop\ \1.jpg without spaces.

PS. I know its a from 4 years ago but I have been trying to figure this out for some time and someone might find this usefull...

夏日浅笑〃 2024-12-27 04:42:54

使用 selenium 将视频上传到 YouTube 的 Python 解决方案。

from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(5) # Wait up to 5 sec before throwing an error if selenium cannot find the element (!important)
driver.get("https://www.youtube.com/upload")
elem = driver.find_element_by_xpath("//input[@type='file']")
elem.send_keys("C:\\full\\path\to\\video.mp4"); # Window$
#elem.send_keys("/full/path/to/video.mp4"); # Linux

注释:
1 - 聪明一点,缓慢但坚定地走;
2 - YouTube 每天最多上传 50 次,但第一天为 100 次;
3 - 截至 2019 年,youtube api 仅限上传 5 个视频 (◔ _◔)

Python solution to upload a video to YouTube using selenium.

from selenium import webdriver
driver = webdriver.Firefox()
driver.implicitly_wait(5) # Wait up to 5 sec before throwing an error if selenium cannot find the element (!important)
driver.get("https://www.youtube.com/upload")
elem = driver.find_element_by_xpath("//input[@type='file']")
elem.send_keys("C:\\full\\path\to\\video.mp4"); # Window$
#elem.send_keys("/full/path/to/video.mp4"); # Linux

Notes:
1 - Be smart, go slowly but surely;
2 - YouTube max uploads per day is 50, but on the first day is 100;
3 - As of 2019, youtube api is limited to 5 video uploads (◔ _◔)

歌枕肩 2024-12-27 04:42:54

尝试一下基于 ytb_up 的 selenium。
灵感来自一堆自动上传 YouTube 视频库

https://github.com/wanghaisheng/ytb-up
您可能需要

  1. 代理支持的

功能自动检测是否需要代理
2. cookie 支持

同一 google 帐户下的多个频道
3.安排时间发布

您可以明确指定每个视频的日期和时间,也可以设置发布策略和每日公众数量,例如,每日数量为4,您有5个视频,那么前4个将在发布后1天发布上传日期,另外1个为上传日期后2天
4.修复谷歌账户验证

give it a shot ytb_up based selenium.
inspired from bunch of auto upload youtube video library

https://github.com/wanghaisheng/ytb-up
features YOU MAY NEED

  1. proxy support

auto detect whether need a proxy
2. cookie support

for those multiple channels under same google account
3. schedule time publish

you can explictly specify a date and time for each video or you can set publish policy and daily public count,for example,daily count is 4,you got 5 videos,then first 4 will be published 1 day after the upload date ,the other 1 will be 2 days after the upload date
4. fix google account verify

睡美人的小仙女 2024-12-27 04:42:54

很简单,用IDE录制即可。上传命令正在运行

it is quite simple, just record it using IDE. Upload command is working

谁的年少不轻狂 2024-12-27 04:42:54

如果页面上有一个带有文件输入的表单,我认为在输入中填充值并使用selenium的python api提交表单很简单。您可以在文档页面上找到一些示例代码

If there is a form with file input on the page, I think it's straightforward to fill value in the input and submit the form with python api of selenium. You can find some sample code on the document page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文