Upload file with Selenium in Python
Asked Answered
P

7

3

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

Puppy answered 8/12, 2011 at 8:23 Comment(2)
O
-3

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

Oskar answered 8/12, 2011 at 8:35 Comment(1)
Question is quite specific, while answer is pointing to hello world example for selenium in java. Down voting it.Vampire
C
9

It can be done via:

element = driver.find_element_by_name("file")
element.send_keys("/home/pavel/Desktop/949IH3GNHAo.jpg")
Coxswain answered 30/6, 2012 at 17:28 Comment(0)
G
1

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')
Gans answered 17/11, 2020 at 16:29 Comment(0)
M
0
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...

Marque answered 13/7, 2016 at 14:26 Comment(0)
L
0

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

Logue answered 23/10, 2019 at 12:44 Comment(0)
A
0

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

Ader answered 26/10, 2021 at 10:25 Comment(0)
E
-2

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

Expire answered 5/7, 2014 at 7:39 Comment(0)
O
-3

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

Oskar answered 8/12, 2011 at 8:35 Comment(1)
Question is quite specific, while answer is pointing to hello world example for selenium in java. Down voting it.Vampire

© 2022 - 2024 — McMap. All rights reserved.