Home > Back-end >  Issues to write a file path on Windows with PyAutoGUI
Issues to write a file path on Windows with PyAutoGUI

Time:02-21

I am writing a simple Python script that goes on a website (with Selenium) and upload a file on the website. I'm using PyAutoGUI to enter the filename and press "Enter" because the website doesn't use an input.

driver.get("https://website_url.com/upload/")
elm = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "file_Picker")))
driver.find_element(By.CLASS_NAME, "file_Picker").click()
pyautogui.write("C:\\Users\\Lulucmy\\PythonProject\\test.png")
pyautogui.press('enter')
time.sleep(2)

The issue is that each time PyAutoGUI write on the upload window, the colon is replaced by a slash :

C/\\Users\\Lulucmy\\PythonProject\\test.png

What I've tried :

  • Replacing pyautogui.write by pyautogui.typewrite
  • Using pyautogui.press(':') and dividing the file path in two parts

I think the issue comes from the keyboard layout, but I couldn't find how to change it on PyAutoGUI. Also, if you could think of a solution without using PyAutoGUI I'd be glad to know it.

Thank you for your help!

CodePudding user response:

No, you don't click on the filePicker. Just send the path of the file and it will work:

driver.get("https://website_url.com/upload/")
elm = WebDriverWait(driver, 20).until(EC.element_to_be_clickable
((By.CLASS_NAME, "file_Picker")))
elm.SendKeys("C:\\Users\\Lulucmy\\PythonProject\\test.png")    
time.sleep(2)

CodePudding user response:

I found an easy way to fix it - but if you have a "cleaner way" to solve the issue I'm happy to hear it.

The problem came from my AZERTY keayboard; PyAutoGUI seems to use the QWERTY layout by default. I used pyperclip to copy the path in the clipboard, and then paste it (ctrl v) using .hotkey with PyAutoGUI:

import pyperclip

driver.get("https://website_url.com/upload/")
elm = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "file_Picker")))
driver.find_element(By.CLASS_NAME, "file_Picker").click()
pyperclip.copy("C:\\Users\\Lulucmy\\PythonProject\\test.png")
pyautogui.hotkey('ctrl', 'v')
pyautogui.press('enter')
  • Related