I cannot upload a file with python using selenium

Viewed 98

I tried to upload a file with python using selenium. This is the code that I use:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time

file = "C:\\Users\\Marcelino\\Downloads\\issues.xlsx"
driver = webdriver.Chrome(executable_path=r'E:\chromedriver.exe')
driver.implicitly_wait(15)
driver.get("https://host.boostmediagroup.com:2083/cpsess3744252593/frontend/paper_lantern/filemanager/upload-ajax.html?file=&fileop=&dir=%2Fhome%2Fsftpgoogletxl%2Fupdate_drive%2Fsource&dirop=&charset=&file_charset=&baseurl=&basedir=")
driver.find_element_by_xpath("/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/form/div[2]/input").send_keys("sftpgoogletxl")
driver.find_element_by_xpath("/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/form/div[4]/input").send_keys("I5ztfn=FR_1K")
driver.find_element_by_xpath("/html/body/div[2]/div/div[3]/div/div/div[2]/div[2]/form/div[5]/div/button").click()
time.sleep(5)
driver.find_element(By.XPATH, "/html/body/div[2]/div/div/div[2]/div[2]/div[2]/button").send_keys(file)

But when I run the code the specific file is not uploaded on the site even if the code was "Process finished with exit code 0" and I don't know what to do anymore, I was tried with pyautogui but I use this bot on a VM and when I run on VM the script is not working because the VM is locked. Any other ideas? Thank you in advanced Here is a video with behavior: Video

1 Answers

To upload the file directly you should use input element with type of file.
Try this:

driver.find_element(By.XPATH, "//input[@type='file']").send_keys(file)
Related