Zyngle
Zyngle

Reputation: 11

Web-Browser asks to upload file

I am trying to automate one of my tasks using python and selenium. I am very new to both, so I may not have the expertise as everyone else. Their is a part in my task where I visit a web page to upload a file that is in my file explorer. I have been doing research on how to do this; however, it doesn't seem to work. Here are some screenshots:enter image description here enter image description here

` from selenium import webdriver from selenium.webdriver.chrome.service import Service as ChromeService from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.select import Select from selenium.webdriver.support import expected_conditions as EC

#this is used to not let chrome close
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

driver=webdriver.Chrome(options=options,service=ChromeService(ChromeDriverManager().install()))
driver.maximize_window()
driver.get("url")

#to be able to click button 
button1 = driver.find_element(By.CLASS_NAME,"button1")
button1.click()

#sign into to website 
username = driver.find_element(By.NAME, "UserName")
username.send_keys('username')

password = driver.find_element(By.NAME, "Password")
password.send_keys('password')

form = driver.find_element(By.ID, "submitButton")
form.submit()

#MFA
button2 = driver.find_element(By.ID, "button2")
button2.click()

WebDriverWait(driver,      20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='duo_iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space()='Send Me a Push']"))).click()

#website and choosing drop-down 
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.ID, "ID"))).click()

dd = Select(driver.find_element(By.ID, "id"))
dd.select_by_value("value")

#click the upload button and file explorer window opens
button3 = driver.find_element(By.ID, "button3")
button3.click()

element = driver.find_element(By.CLASS_NAME, "class_name")
driver.execute_script("arguments[0].click();", element)

#selecting the file to upload into website - stuck here




print("Application title is ",driver.title)
print("Application url is ",driver.current_url)

`

Upvotes: 0

Views: 211

Answers (1)

I.sh.
I.sh.

Reputation: 2128

You need to locate the input element on the webpage,
and then use the send_keys method to send the file path as a string:

# Locate the file input field
file_input = driver.find_element(By.ID, "fileInputId")  # Replace with the actual ID of the file input element

# Specify the file path to upload
file_path = "/path/to/your/file.txt"  # Replace with the actual file path

# Use send_keys to set the file path in the input field
file_input.send_keys(file_path)

# Submit the form or perform any necessary actions to complete the upload

Upvotes: 4

Related Questions