cxyz
cxyz

Reputation: 843

Uploading File Using Selenium

Am new to selenium.My requirement is to automate uploading of a csv file using a browse button.The issue am facing is i have 2 forms with two browse buttons,with same name and same value.So i have to click browse button based on form(form names are different).Below is my sample code

Script to click browse button:

#include <IE.au3>
; Internet Explorer is partly integrated in shell.application
$oShell = ObjCreate("shell.application")    ; Get the Windows Shell Object
$oShellWindows=$oShell.windows          ; Get the collection of open shell Windows
$MyIExplorer=""
for $Window in $oShellWindows       ; Count all existing shell windows
  ; Note: Internet Explorer appends a slash to the URL in it's window name
  if StringInStr($Window.LocationURL,"http://") then
      $MyIExplorer=$Window
      exitloop
  endif
next
$oForm = _IEGetObjByName ($MyIExplorer, "document.forms['UploadForm'].elements['browsebutton']")
_IEAction($oForm, "click")

Below is my script to upload csv file

WinActivate("File Upload");
Local $file ="C:\Work\selenium\abc.csv"
ControlSetText("Choose file", "", "Edit1", $file )
ControlClick("File Upload", "", "Button2")

Am calling the code as below in my java class:

Process proc = Runtime.getRuntime().exec("C:\\bowsebutton.exe");
Process proc1 = Runtime.getRuntime().exec("C:\\test3.exe");

When i run seleinum Am not able to click the browse button at all.But if i manually click the browse button the csv gets uploaded automatically and file gets submmitted.

Am not able to figure out why the browse button is not being clicked.Any help would be appreciated.

Upvotes: 0

Views: 1418

Answers (1)

Scott Loveland
Scott Loveland

Reputation: 31

You have to use FindElement to get the dialog to popup. I don't know what it looks like in Java but it can't be too far off from what it is in C#

    InternetExplorerDriver driver = new InternetExplorerDriver();

    //Do stuff obviously ....

    driver.FindElement(By.XPath("//input[@type='file']")).SendKeys(@"c:\somefile.txt");

Upvotes: 1

Related Questions