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.

1
  • Can you check siblings/ancestors for <input type="file">? Commented Feb 7, 2017 at 9:50

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

As i say before, there are not element with InputTypeFile. I will try your code
@Sergeymoroz Use AutoIT
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
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");
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.