0

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!

2 Answers 2

1

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)
2
  • Thank you for your answer, unfortunately it doesn't work as the upload on the website is not an "input" and it just opens a window, asking to upload the file. 'WebElement' object has no attribute 'sendText'
    – Lulucmy
    Feb 20, 2022 at 20:45
  • @Lulucmy My bad. I meant SendKeys, not sendText.
    – Tal Angel
    Feb 20, 2022 at 21:22
0

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')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.