4

From Selenium, I managed to automate my tasks for a website. However I ran into a problem:I need to upload a file to my web-page before submitting the form. It is NOT an option to write the uploaded file into it's input element, because it is more complicated than this. So basically I need to launch the FileUpload dialog by clicking a button, sendKeys there, and then close it by clicking on Ok. I am wondering if this is even possible using just Selenium? I am using it from python (so I don't have access to Robot class)

I tried so far:

element.click()
time.sleep(5)
alert = driver.switch_to.alert
alert.send_keys("path.to.myfile.txt")
alert.accept()

(nothing happens - I mean, the file open dialog works fine, but it does not send the keys )

I also tried:

alert = driver.switch_to.alert
buildu = ActionChains(driver).send_keys('path.to.my.file.txt')
buildu.perform()

(also not working)

Maybe I am looking at it wrong... Maybe the alerts is not a good approach? Do you have any idea? I would prefere not having to use AUTOIT (for my own reasons)

So my goal is to click on a link element (DONE) then the link opens the File Upload open file dialog (DONE), then I need to be able to enter a text in the only textBox of the new window, and click on the Ok button

EDIT This is the Open File dialog that shows up.

enter image description here

All I want to do is send the filename directly to the window (the textBox is focused when the dialog shows up, so no need to make more actions). After I send the keys (text) I need to be able to click on the Open button

user1137313
  • 2,390
  • 9
  • 44
  • 91
  • Hi, can you upload a screenshot of the dialog? – Shah Dec 10 '15 at 09:45
  • Sure, just a second... – user1137313 Dec 10 '15 at 09:54
  • Please have a look here [link](http://stackoverflow.com/questions/11256732/how-to-handle-windows-file-upload-using-selenium-webdriver) – Shah Dec 10 '15 at 10:06
  • You can not use just `Selenium` for this purpose as file upload prompt window is not a web element. And you also can not sent text just to new window- you should `find` input field first – Andersson Dec 10 '15 at 11:52
  • you'd better try to use Python `requests` module and send file within http POST request – Andersson Dec 10 '15 at 11:54
  • @Andersson... I thought about that (from the very begining) but the post that the page sends, contains besides the regular inputs, 2 dynamic, session like, IDs, that I cannot generate. So this approach wont work. That is why I want to automate it with selenium – user1137313 Dec 10 '15 at 13:15
  • There are preferences that allow to download files with `Firefox Profile` avoiding prompt window. Maybe there are some preferences that allow to upload files also... – Andersson Dec 10 '15 at 13:26

2 Answers2

2

I don't know if this is restricted to windows 10 or works slightly different in other Windows versions but you can use the following code/idea.

  1. Open the windows shell with win32com.client.Dispatch("WScript.Shell")
  2. Send Tab keys to navigate through the open file dialog
  3. Paste your absolute file path into the top of the dialog and also into the file selection text field and send enter key when tab-navigating to the "open" button
  4. Make a sleep of at least 1 second between each action.. otherwise Windows blocks this action.

    import win32com.client as comclt
    

    ...

        def handle_upload_file_dialog(self, file_path):
            sleep = 1
            windowsShell = comclt.Dispatch("WScript.Shell")
            time.sleep(sleep)
            windowsShell.SendKeys("{TAB}{TAB}{TAB}{TAB}{TAB}")
            time.sleep(sleep)
            windowsShell.SendKeys("{ENTER}")
            time.sleep(sleep)
            windowsShell.SendKeys(file_path)
            time.sleep(sleep)
            windowsShell.SendKeys("{TAB}{TAB}{TAB}{TAB}{TAB}")
            time.sleep(sleep)
            windowsShell.SendKeys(file_path)
            time.sleep(sleep)
            windowsShell.SendKeys("{TAB}{TAB}")
            time.sleep(sleep)
            windowsShell.SendKeys("{ENTER}")
    

    ...

Alien
  • 15,141
  • 6
  • 37
  • 57
0

You can create a Java program that will paste the filename and press the enter key. I had this same problem exactly. This is how I implemented:

package myaots_basic;
import java.io.*;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
public class trial {

public static void main(String[] args) throws AWTException {
    System.out.println("HELLO WORLD");
    StringSelection attach1 = new StringSelection ("C:\\My Office Documents\\Selinium projects\\Data\\attachment1.doc");
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(attach1, null);
    Robot rb1 = new Robot();
    rb1.delay(3000);
    rb1.keyPress(KeyEvent.VK_CONTROL);
    rb1.keyPress(KeyEvent.VK_V);
    rb1.keyRelease(KeyEvent.VK_V);
    rb1.keyRelease(KeyEvent.VK_CONTROL);
    rb1.delay(500);
    rb1.keyPress(KeyEvent.VK_ENTER);    // press Enter
    rb1.keyRelease(KeyEvent.VK_ENTER);
}

}

Name it to trial.jar. Make this class a an executable.

Then in your python code just add a simple step: import subprocess subprocess.call("java -jar trial.jar", shell=True)

Nisheeth
  • 271
  • 4
  • 13