How to Upload File ( Picture ) with Selenium, Python

How to upload file ( picture ) with selenium, python

Upload input control opens a native dialog (it is done by browser) so clicking on the control or browse button via Selenium will just pop the dialog and the test will hang.

The workaround is to set the value of the upload input via JavaScript (in Java it is done via JavascriptExecutor) and then submit the form.

See this question for sample in C#, I am sure there's also a way to call JavaScript in Python but I never used Selenium Python bindings

How to upload a file (image) with Selenium using python on Linux?

If you check the input element the style attribute present as display: none;
That's the reason it is not interacting even using the valid locator.

You need the change the style of the element to display: block; and then try to upload the file using send_keys()

Use java script executor to change the style.

fileupload=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div[qa-id='dropZone']>input[type='file']")))
driver.execute_script("arguments[0].style.display = 'block';",fileupload)
fileupload.send_keys(path/to/file)

Hope this code will works for you.

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

How to upload file using python selenium-webdriver when there is no input type but instead it has a button type in HTML?

There is a hidden input with type=file. To upload file using Selenium yo have to send keys to input[type=file]:

browser.find_element_by_css_selector(".file-upload-input input[type=file]").send_keys('C:\\Users\\Desktop\\test.png')

Use WebDriverWait to wait element to be present in the DOM:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# ...

wait = WebDriverWait(driver, 5)

wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".file-upload-input input[type=file]"))).send_keys('C:\\Users\\Desktop\\test.png')

Uploading a picture on Instagram using Selenium

I see there is a input tag with type as a file.

input[type='file']

if we see any css with matching above css selector, then we can use send_keys to send the file, we do not need auto it or any other tool. it would work in windows/unix/mac etc.

Code :

def upload(self, imgage, text):
upload_btn = driver.find_element(By.CSS_SELECTOR, "input[type='file']")
time.sleep(2)
upload_btn.send_keys('full path of the file which is to be uploaded')

Upload profile picture on Microsoft with selenium, python ()

There are few steps that you need to follow in order to upload a pic :

  1. you need to switch driver focus to new tab
  2. Click on Add a photo.
  3. Upload a pic.
  4. Assert Successful Message.

Code :

#4. Upload Picture
windows_before = browser.current_window_handle
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.ID,"mectrl_currentAccount_picture_profile_picture"))).click() # this is from #3
windows_after = browser.window_handles
new_window = [x for x in windows_after if x != windows_before][0]
browser.switch_to.window(new_window)
browser.find_element(By.CSS_SELECTOR, "button[id='profile.profile-page.profile-pic-section.edit-picture']").click()
browser.find_element(By.CSS_SELECTOR, "button[id='button[id='profile.edit-picture.upload-button']']").click()
browser.find_element(By.CSS_SELECTOR, "input[type='file']").send_keys("path of the file to be uploaded")
#Assert your msg

Upload file with Selenium Webdriver Python

The problem is - you are sending keys to the div element which is not "interactable", does not accept the keys - hence the "cannot focus element" error.

The idea behind the solution you've linked is to send keys to the input element with type="file" that is responsible for the file upload. Find this element in your HTML and send keys to it.

Note that this element could be invisible. In this case, you should first make it visible for the send_keys() to work.


Update:

Okay, now we at least know which element is our desired one:

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

Since you have troubles locating this element, either try waiting for it:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

file_upload = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "fileToUpload2"))
)
file_upload.send_keys('/Users/home/Downloads/1-Students-and-Parent-Email.csv')

Or/and, check if this element is inside an iframe - if it is, you would need to switch into the context of the iframe and only then perform the element search.

How to upload an image with selenium python?

  1. You need to install AutoIt "pip install -U pyautoit" through the cmd screen

  2. type "import autoit" on your script page

  3. Type the following before the file dialog pops up in your script:

    autoit.win_active("Open")
    autoit.control_send("Open","Edit1",r"Path with filename")
    autoit.control_send("Open","Edit1","{ENTER}")

For you reference https://pypi.python.org/pypi/PyAutoIt/0.3

Please Note : Section 3 will be implemented after click on the button.
Hope this help you.



Related Topics



Leave a reply



Submit