0

I have span link to upload images. When i click this link it opens a Chrome Window to select a file. But Selenium can't do this automatically. How can I handle this window and choose a file?

choose_photo = driver.find_element_by_id("form-2033-innerCt")
choose_photo.click()
choose_photo.send_keys("C:\\Users\\Support\\AppData\\Roaming\\Skype\\My Skype Received Files\\1.png")

And this is the HTML element to click

<span id="fileuploadfield-2034-button-btnInnerEl" data-ref="btnInnerEl" unselectable="on" class="x-btn-inner x-btn-inner-default-small">Choose photo</span>

Screenshot:

enter image description here

Note that the HTML element is not type = file, as you can see.

NarendraR
  • 7,577
  • 10
  • 44
  • 82
SergeyMoroz
  • 117
  • 3
  • 11

3 Answers3

2

In HTML the common way to upload a file is to use an input type=file. I am guessing that in your case the file input is hidden and clicking the span triggers it. You can try to locate the hidden input and type into it.

See also How to handle windows file upload using Selenium WebDriver?

Community
  • 1
  • 1
Nir Levy
  • 4,613
  • 2
  • 34
  • 47
1

>

driver.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")

>

OR Try to upload file using AutoIT, If this one does not work for you then use AutoIT for upload file

Kuldeep Yadav
  • 107
  • 1
  • 14
  • As i say before, there are not element with InputTypeFile. I will try your code – SergeyMoroz Feb 07 '17 at 10:17
  • @Sergeymoroz Use AutoIT – Kuldeep Yadav Feb 07 '17 at 10:20
  • choose_photo = driver.find_element_by_id("form-2033-innerCt") choose_photo.click() autoit.win_wait_active("Open") print(autoit.win_get_title_by_handle()) autoit.send_keys(os.path.join("C:\\Users\\Support\\AppData\\Roaming\\Skype\\", "1.png")) autoit.send("{ENTER}") Its also not working – SergeyMoroz Feb 07 '17 at 10:27
1

Steps to download and install AutoIt :-

  1. Download link = http://www.autoitscript.com/site/autoit/downloads/

  2. Install

  3. Go to your program menu and look at the AutoIt folder and open according to your system

  4. Now download AutoIt script editor and install, download link = http://www.autoitscript.com/site/autoit/downloads/

Steps to use AutoIT :-

  1. Identify the Windows control

  2. Build an AutoIt script using identified windows control

  3. Compile the .au3 script and convert it into .exe file

  4. Call the .exe file into the Selenium test case

Below is AutoIt Script :-

Wait 10 seconds for the Upload window to appear

WinWait("[CLASS:#32770]","",10)

Set input focus to the edit control of Upload window using the handle returned by WinWait

 ControlFocus("File Upload","","Edit1")

 Sleep(2000)

Set the File name text on the Edit field

  ControlSetText("File Upload", "", "Edit1", "SomeFile.txt")

  Sleep(2000)

Click on the Open button

ControlClick("File Upload", "","Button1");
  1. Compile the .au3 script and convert it into .exe file

  2. Call the .exe file into the Selenium test case e.g.

    Runtime.getRuntime().exec("D:\AutoIt\AutoItTest.exe");
    
NarendraR
  • 7,577
  • 10
  • 44
  • 82
Kuldeep Yadav
  • 107
  • 1
  • 14