user1400538
user1400538

Reputation: 865

Automating "Browse" button event in Selenium

I need to automate the "Browse" button click from Selenium.

enter image description here

For this, I have tried

driver.findElement(By.xpath("//*[@id=\"dnn_ctr383_View_filename\"]")).click();

and

driver.findElement(By.cssSelector("Css path")).click();

Both gives me org.openqa.selenium.NoSuchElementException: Unable to locate element: exception.

I have seen this link here where the author suggest to use AutoIT, but in step 2, the script, the author has created is for IE. Can someone please suggest, how I can automate the "Browse" button click in firefox?

Any help is much appreciated.

Upvotes: 1

Views: 4465

Answers (2)

kshitij Nigam
kshitij Nigam

Reputation: 44

There are two things that u need to consider here:

  1. Clicking the browser button: Usually handled by an alert or popup, if the driver is not able to find the element by xpath(which u have acquired from firebug or chrome's inspect element) you should consider looking for iframes in the page source. If an element is in a different frame altogether , u need to switch frames in order to find the element like this

    WebElement frame = driver.findElementById("name_of_iframe");

    driver.switchTo().frame(fr);

now you can find your element using xpath or css selector like u did. Once completed u can move out of the frame by:- driver.switchTo().defaultContent();

  1. Uploading the file from desktop: Since selenium works only on the html in the browser session u invoked from the driver, it can't perform operations on your desktop.Once you are able to click the browser button u can use Auto it(works only on windows) or Sikuli(works with mac, linux, windows and even android) to simulate your events based on the position of your upload button.

Hope this helps

Upvotes: 0

Amey
Amey

Reputation: 8548

Directly send the file path to the id, like so

driver.findElement(By.id("dnn_ctr383_View_filename")).sendKeys("C:\\path\\to\\file");

The above step is the answer for your first two steps

  1. Click on Browse
  2. Select a file to upload

For the third step(click upload), looking at the screen capture I do not see any button which says "Upload". So just click "Save" and I assume that your file will successfully get uploaded.

Upvotes: 3

Related Questions