How to resolve 'Cannot convert from void to WebElement' error in Selenium



  • 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.



  • 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



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2