1

My code is as below. when i am executing it its giving me error as:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Type mismatch: cannot convert from List<WebElement> to WebElement

at Learning_Selenium.Selenium_Class.main(Selenium_Class.java:13)

Let me know if any solution is there. Thanks in advance...

package Learning_Selenium;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Selenium_Class {
    public static void main(String[] args) {
         // TODO Auto-generated method stub
         WebDriver driver = new FirefoxDriver();
         driver.get("http://google.com");
         System.out.println(driver.getTitle());
         WebElement element = driver.findElements(By.name("q"));
         element.sendKeys("Cheese\n");
         element.submit();
         driver.close();
     }
 }

closed as unclear what you're asking by Peter M., IAmMilinPatel, ECiurleo, Bruce McLeod Feb 11 '16 at 10:20

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Why do you think that you can convert list to element? List is a collection. Same as converting a book (list of characters) to a single character. – Peter M. Feb 4 '16 at 18:41
3

FindElements returns a collection of elements. What you need to do is use the singular "FindElement" to get a single instance of an element to work with.

WebElement element = driver.findElement(By.name("q"));
1

You have to use driver.findElement(By.name("q")) instead of driver.findElements(By.name("q")) when searching for single web element.

1

This is one of the basic Webdriver concepts. When you are searching for a single element- like a single button that has to be clicked on, you use the findElement( ) , along with a locator mechanism, to find that element.

For example-

WebElement ele1= driver.findElement(By.name("btn1"));

or in Python

ele1= driver.find_element_by_id('btn1')

You use the findElements( ) method, when you have to find a list of elements. We often use it when there are multiple instances of a web element and you need to find all of them.

For example, if I need to get all the hyperlinks in a webpage, I can get it by using the findElements() method and searching for the anchor tag.

WebElement ele2 = driver.findElements(By.tagname("a"));

Now, since this would return multiple elements,you will again get an exception as above. We would need a placeholder, that holds multiple elements. So we define the Web Element to of List type (in both Java and Python)

In Java

List<WebElement> list1= driver.findElements(By.tagname("a"));

In Python

list_webele = driver.find_elements_by_tag_name('a')

Hope this helps you in understanding.

1

You are using findElements method which will return the list of elements. Where you should use findElement method which return single webElement

Your code should be

 WebElement element = driver.findElement(By.name("q"));
1

You are trying to get array of elements in single element, so compiler throwing an exception.

Try:

package Learning_Selenium;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.firefox.FirefoxDriver;
 public class Selenium_Class {

public static void main(String[] args) {
    // TODO Auto-generated method stub
       WebDriver driver = new FirefoxDriver();
       driver.get("http://google.com");
       System.out.println(driver.getTitle());
       WebElement element = driver.findElement(By.name("q"));
       element.sendKeys("Cheese\n");
       element.submit();
       driver.close();

}

When you use driver.findElements() it means it will get all webElements which argument finds in this query, if you tend to get all element then use :

List<<WebElement>> element = driver.findElements(By.name("q"));

this will get all elements stored in array.

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