'Upload photo button not working in Selenium Webdriver

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



Solution 1:[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.

Solution 2:[2]

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");

Solution 3:[3]

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") ;

Solution 4:[4]

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.

Solution 5:[5]

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);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 GPT14
Solution 2 Ishita Shah
Solution 3 cruisepandey
Solution 4 Ratmir Asanov
Solution 5 S.B