I am getting below error:


Type mismatch: cannot convert from void to WebElement.


WebElement   uploadElement = (driver.findElement(By.xpath("//input[@id='HorizontalLogoPath']"))).click();

And this line of code is part of public void upload() function.

  • I am getting error for this line asType mismatch: cannot convert from void to WebElement.And this line of code is part of public void upload() function Oct 4, 2019 at 8:06
  • does your upload() function return something? Oct 4, 2019 at 8:47

2 Answers 2


Method click() that you invoke on located element does not return anything. It is void (you probably used this "type" in some of your methods declaration like public static void main(), etc).

So as that method is called the last in your right-hand side of assignment, Java assumes you're trying to assign what the method returns to what you have on left-hand side.

Hence, type mismatch occurs.

The proper approach would be first assign an element (here it wouldn't be any issue since findElement() is called last on the right-hand and it returns WebElement)

WebElement uploadElement = driver.findElement("//input[@id='HorizontalLogoPath']");

and then invoke click() of your uploadElement


First initialize the WebElement and use it further for click or sendkeys() function.

As like below:

public void uploadFn()
       WebElement   uploadElement = driver.findElement(By.xpath("//input[@id='HorizontalLogoPath']"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.