0

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)

`

1
  • You'll want to find the <input type="file"> tag and use sendKeys('path_to_file'); Selenium will set the input's value attribute to the file path. Then submit the form. You can use the convenience method of .submit() on the same element. Commented Sep 5, 2023 at 21:04

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, thank you for your comment. I have tried what you have suggested; however, I am still running into errors. this is the HTML for the Browse... button: "<span tabindex="0" class="file-selector-browse" xpath="1">Browse...</span>" using: element = driver.find_element(By.CLASS_NAME, "class_name") driver.execute_script("arguments[0].click();", element) helps open the file explorer. Once the file explorer opened I tried using what you have suggested and the file explorer does not locate the file at all.
don't click the button that opens the browse dialog... that's outside of the DOM and beyond Selenium's control. Sendkeys to the input of type=file and then submit the form.
@pcalkins got it to work now. the file is inputted and everything now my issue I am running into is being able to actually upload the file. Here is what I have tried: #press upload on website upload_button = driver.find_element(By.CSS_SELECTOR, "button[class='btn btn-primary']") upload_button.click() here is the html: <button data-testid="modal-footer-button-primary" type="button" class="btn btn-primary" xpath="1" style="">Upload</button> keep getting the error: ElementClickInterceptedException I am not sure how to trouble shoot. thank you guys again!
There is a convenience method for this which you can use on any form element. So after sending keys, you can just use .submit() on that same element. Submit method: selenium.dev/selenium/docs/api/java/org/openqa/selenium/… If you need to click the button for testing, you'll want to find out why another element is grabbing the click. (sometimes it's overlaying it... the exception should list that element that's grabbing the click, but best to create a new question for that...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.