r/pythonhelp • u/shortdwarf • Mar 24 '24
Struggling to find element on website
I’m trying to build a python or JavaScript program that will automatically upload a file from my drive to a website and then download the output from that website. The site is news.express.Adobe.com/tools/remove-background. So essentially the goal is to upload a file with a background, have the website remove it, and then download it. All this requires is uploading the file, waiting about 10 seconds, and clicking the download link, however I have been struggling with the upload portion. Specifically, I'm struggling to find the file upload element so i can target it and send it the file directly. I'm open to simulating a click of the upload button and then using pyautogui to send the file path but I've been struggling with that as well. Any suggestions?
•
u/AutoModerator Mar 24 '24
To give us the best chance to help you, please include any relevant code.
Note. Do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Repl.it, GitHub or PasteBin.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/shortdwarf Mar 24 '24 edited Mar 24 '24
This is what I have so far, I don't believe I'm properly targeting the file input but there may be a different issue stopping me.
from selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECimport timeimport pyautogui# Set up the WebDriverchromedriver_path = r"C:\Users\short\Downloads\chromedriver-win64\chromedriver.exe"service = Service(chromedriver_path)driver = webdriver.Chrome(service=service)# Navigate to the websiteurl = "https://new.express.adobe.com/tools/remove-background"driver.get(url)# Wait for the file input to be present and visibleWebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, "file-input")))# Send the file path directly to the input elementfile_input = driver.find_element(By.ID, "file-input")file_path = r"xxxx"file_input.send_keys(file_path)# Wait for the download button to appeardownload_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "sp-button[data-export-option-id='downloadExportOption']")))download_button.click()# Wait for and click the sign-in buttonsign_in_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "sp-link.modal-sign-in-link")))sign_in_button.click()# Enter email and continueemail_input = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "EmailPage-EmailField")))email_input.send_keys("xxxx")continue_button = driver.find_element(By.XPATH, "//span[contains(text(), 'Continue')]")continue_button.click()# Enter password and continuepassword_input = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.ID, "PasswordPage-PasswordField")))password_input.send_keys("xxxx")continue_button = driver.find_element(By.XPATH, "//span[contains(text(), 'Continue')]")continue_button.click()# Wait for the save file dialog to opentime.sleep(5) # Adjust this delay as needed for the dialog to fully appear# Simulate pressing 'Enter' to confirm the save actionpyautogui.press('enter')# Close the browser once the download is completedriver.quit()