How to upload a file in Selenium with no text box

后端 未结 4 1973
北海茫月
北海茫月 2020-12-01 18:58

I have been looking for a solution to uploading a file in Selenium 2.

The problem is that the web element that I\'m trying to upload is usable in two ways: Drag and

4条回答
  •  臣服心动
    2020-12-01 19:57

    I found the only way I could get it working was to use AutoIt (thanks to answers by LittlePanda and user3903359).

    I improved on the script as I found performing any other actions while the test was running could stop it working. The trick was to find the window then make it active before entering the text.

    The timeout is to prevent multiple AutoIt scripts hanging around in the background which means when you stop the test and try to do your own work they then kick off and try to start typing!

    Note that the window is named differently in different browsers (e.g. "Open" in Chrome).

    $windowHandle = WinWait("Choose File to Upload", "", 3) ; 3 second timeout - NB the window name will be different in different browsers!
    
    If $windowHandle == 0 Then
       MsgBox(0, "", "Upload popup not found")
    Else
       ;MsgBox(0, "", "Upload popup found: " & $windowHandle)
       WinActivate($windowHandle)
       Send("C:\\path\to\myfile.txt")
       Send("{ENTER}")
    EndIf
    

    Running the AutoIt script from Java is I assume as per all the other answers:

    Runtime.getRuntime().exec("MyAutoItScript.exe");
    

    Running the AutoIt script from C#:

    var process = Process.Start(@"C:\\path\to\myAutoItScript.exe");
    process.WaitForExit();
    Thread.Sleep(200); // IE fix for Modal dialog present exception
    

提交回复
热议问题