1

I am getting below error:

Error:

Type mismatch: cannot convert from void to WebElement.

CODE:

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

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 – shwetali diwanji Oct 4 at 8:06
  • does your upload() function return something? – Niels van Reijmersdal Oct 4 at 8:47
3

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

0

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']"));
       uploadElement.click();
       uploadElement.sendKeys("D:\\LOGO\\HorizontalLogoPath");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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