0
  WebElement UploadImg = driver.findElement(By.xpath("//*[@id='file_upload_button']"));
  UploadImg.click();
  WebElement frame =driver.switchTo().activeElement();
  frame.sendKeys("d:\5.jpg");

This code just open system window but it doesn't select any Photo/File

3
  • Hi. You cannot automate any windows based dialog/windows using selenium. Selenium is only meant for browser. If you want to upload the file then use sendkeys method to type the path of the file and perform submit button press. Commented Sep 26, 2013 at 10:25
  • @user2798616 Don't you have a text box beside UploadImage button for entering the path of the file you are trying to upload? If you have, then no need to click on the upload button. directly paste the file path using sendKeys into that text box. Commented Sep 26, 2013 at 15:36
  • @Vinay : Website doesn't have any textbox beside UploadImage button for entering path ...!It just have Button, So how can i send path using this button Commented Oct 8, 2013 at 9:45

4 Answers 4

5

Run This code :

JavascriptExecutor js = (JavascriptExecutor) driver; 
js.executeScript("arguments[0].setAttribute('style', arguments[1])",` driver.findElement(By.xpath("//input[@type='file']")), "0");
js.executeScript("arguments[0].setAttribute('class', arguments[1])", driver.findElement(By.xpath("//input[@type='file']/../../div[2]")), "a");
driver.findElement(By.xpath("//input[@type='file']")).sendKeys("Your Path to the file your system");

Explanation: Every Browse button has a <input> tag in DOM in hidden state . By using the below lines of code, we just change the class and style attributes of the tags enclosing that <input> tag so that it becomes visible and a sendKeys() command can be performed on it. After that when you do a sendKeys with the absolute path of the image/file you want to upload.

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

Comments

4

Try changing your code to:

  WebElement UploadImg = driver.findElement(By.xpath("//*[@id='file_upload_button']"));
  UploadImg.sendKeys("d:\5.jpg");

Comments

3

Kyop's answer is correct, though for this to work the element you are finding needs to be in the form <input type="file" id="file_upload_button">. Could you post an HTML snippet of the relevant code to verify this?

Also why complicate things with XPath for an id find? Is there some benefit over just using driver.findElement(By.id("file_upload_button")) instead?

Source: this post

3 Comments

Nope, kyop's code is not working. You can check that with link ":classifieds.sandhira.com/form/boats-or-ships/post-classified " As i want to upload photo in this website using 'Upload image Button' within page
Try; WebElement UploadImg = driver.findElement(By.id("file_upload"));. You need to target the input with type="file" not type="button".
can u pls do this for above mentioned link. As i m not able to handle this.
0

I am using the idea that @Raavan explained.

For instance: On this page https://touch.facebook.com/marketplace/selling/item/ I am using the following code with success:

c = nav.find_element_by_xpath("//input[@type='file']")
c.send_keys("/home/izaias/Documentos/Script/img/133722.jpg")

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.