1

Upload photo button not working in Selenium Webdriver

What I have already tired

driver.findElement(uploadPhotoBtn).sendKeys("E:\\photo.png");

Also tried the Robot function

    driver.findElement(uploadPhotoBtn).click();
    StringSelection ss = new StringSelection(logoPath);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

Same Robot function working for another upload button, but while trying to use here, the .click not working that is why unable to use the Robot function.

HTML page source:

> <div ng-show="!status.uploading" ng-class="{ '!isMobile':
> 'mewe-type-1' }" class="uploader-able !isMobile"><!-- ngIf: isMobile
> --><!-- ngIf: !isMobile --><button ng-if="!isMobile" class="btn-action radius ng-scope">Upload Photo</button><!-- end ngIf: !isMobile
> --><input capture="camera" accept="image/*" name="image" type="file" fileread="fileread" file="file" class="ng-isolate-scope"></div>

Console log:

org.openqa.selenium.WebDriverException: unknown error: Element ... is not clickable at point (314, 477). Other element would receive the click: (Session info: chrome=66.0.3359.181) (Driver info: chromedriver=2.35.528161

Al Imran
  • 882
  • 7
  • 29

6 Answers6

2

This is example of how to add file to upload button, don't quite sure what is path of button, but You have to find element with <input type="file"> element and interact with it:

WebElement uploadPhotoBtn = driver.find(By...); //type="file" 

File file = new File(path);
uploadPhotoBtn.sendKeys(file.getAbsolutePath());

...and this is how to get source, if You have some kind of preview

elementPreview.findElement(By.tagName("img")).getAttribute("src");

Hope this helps,

Kovacic
  • 1,473
  • 6
  • 21
  • Thanks it's working fine for me :) `WebElement elem = driver.findElement(By.xpath("//input[@type='file']")); File file = new File(logoPath); elem.sendKeys(file.getAbsolutePath());` – Al Imran May 28 '18 at 03:17
  • Glad I could help. – Kovacic May 28 '18 at 06:30
1

A couple of things:

1) The "Element is not clickable" error means that the upload button is covered somehow. That could be it is disabled, behind some cover, or my favorite, the whole page clear div. Make sure that the button you're trying to click is truly available for clicking...

2) For the .sendKeys() to work, you need to point to the <input type="file"> element. Based on the variable name, you're trying to point to the <button> webelement instead.

GPT14
  • 809
  • 6
  • 13
MivaScott
  • 1,763
  • 1
  • 12
  • 30
  • 1
    1. Element is clickable, as I'm able to click manually. I have also used sleep/implicit wait(wait for clickable) for being clickable. 2. `By uploadPhotoBtn = By.xpath(""); driver.findElement(uploadPhotoBtn).sendKeys("E:\\photo.png");` I already used it but not working. Is that ok? – Al Imran May 24 '18 at 03:13
0

You need to 'type' your file into the input element. Your above send keys command isn't quite right.

Code you can try:

driver.findElement(By.xpath("//input[@name="image"]")).sendKeys("Image Path Here") ;
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
TomH
  • 2,581
  • 1
  • 15
  • 30
0

My answer was criticized at one SO post by @JimEvans, who is a core contributor to the Selenium web automation framework. I learned something from him. This is what he had to say about upload button with <input type="file">.

If you are trying to upload a file, and the page in question uses the standard upload mechanisms provided by HTML, you can do this directly with Selenium itself. The standard HTML mechanism is with an <input type="file"> element. Once you’ve found that file upload element on the page, you can use element.sendKeys("full/path/and/file/name/here");. This is documented in Step 10 of the algorithm for the Element Send Keys command of the W3C WebDriver Specification and is used in several file upload tests in the Selenium project’s test code example.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

As according the Error which you are getting, try to solve it by any of below, replacing click event:

Actions act = new Actions(wd);
act.moveToElement("Your Webelement").click().perform();

OR you can operate with JavaScript functionality,

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", "Your Webelement");
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51
0
WebElement upload = d.findElement(By.xpath("//*[@name='filename']"));
Thread.sleep(2000);
Actions action = new Actions(d);
action.moveToElement(upload);
action.click().build().perform();
Thread.sleep(2000);
S.B
  • 13,077
  • 10
  • 22
  • 49