0

I am unable to upload a file to a website using selenium python, My code runs well till Clicking the upload button and opening the Windows file browser frame.
sendkeys doesnot work
AutoIT doesnot work
How do i use this. I tried it but it doesnot works?

browser.find_element_by_xpath("//input[@type='file']").send_‌​keys(filepath)

driver = webdriver.Chrome("C:/Users/path/Downloads/path/chromedriver.exe")
driver.get("http://abc.xys")
elem = driver.find_element_by_id("id_username")
elem.send_keys(user)
elem = driver.find_element_by_id("id_password")
elem.send_keys(pwd)`enter code here`
elem.send_keys(Keys.RETURN)
Upload_button = driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/div[2]/button").send_keys(r'''C:\Users\path\Pictures\test22.jpg''')

I expect the image file to be uploaded

Valentino
  • 7,291
  • 6
  • 18
  • 34
  • The `.send_keys` method is for your `webdriver` I believe. The script you wrote only connects to chromium. Once you go to your windows dialog box, that's `winauto32` already? that's why `.send_keys` doesn't work. – Joe Jul 08 '19 at 14:19
  • Yes I found the Upload button element using the path but i am stuck after that – Ummulkhair Kadri Jul 08 '19 at 14:33
  • Yes, exactly, because opening the dialog box is out of chromium already. `Selenium` only controls your chromium. So I suggest explore `pyautogui` as well to type on that dialog box and click upload? – Joe Jul 08 '19 at 14:47
  • I tried driver.find_element_by_xpath("/html/body/div[2]/div/div[1]/div[2]/button").send_keys(os.getcwd()+r'''C:\Users\path\Pictures\test22.jpg''')..doesnot helps – Ummulkhair Kadri Jul 08 '19 at 15:32
  • `.send_keys()` only works on `input` webelements. Modify your xpath such that it points to the input element of your upload text box. – GPT14 Jul 08 '19 at 15:45
  • 1
    @GPT14 i tried this : browser.find_element_by_xpath("//input[@type='file']").send_‌​keys(filepath)..didnt work either – Ummulkhair Kadri Jul 09 '19 at 05:26
  • can you try with forward slashes / or double backward slashes \\ in your file path? eg: `"C:\\Users\\path\\Pictures\\test22.jpg"` – GPT14 Jul 09 '19 at 05:52

0 Answers0