Home > database >  WebElement and drive.driver little tricky
WebElement and drive.driver little tricky

Time:02-06

I am trying to understand what would it be the right thing to do in here and i do not fully understand it.

Take a look:

String <WebElement> a = drive.driver.getTitle(By.xpath("somelocator"));
List <WebElement> a = drive.driver.findElement(By.xpath("somelocator"));
Boolean <WebElement> a = drive.driver.switchTO(By.xpath("somelocator"));
Map <WebElement> a = drive.driver.quit(By.xpath("somelocator"));
List <WebElement> a = drive.driver.findElements(By.xpath("somelocator"));

What would it be the right thing to do? Why?

CodePudding user response:

The only 2 legal expressions here are

List <WebElement> a = drive.driver.findElement(By.xpath("somelocator"));
List <WebElement> a = drive.driver.findElements(By.xpath("somelocator"));

While only 1 expression here is making sense

List <WebElement> a = drive.driver.findElements(By.xpath("somelocator"));

Because this expression:

drive.driver.findElements(By.xpath("somelocator"));

Returns a List of WebElement objects that will match what you defined on the left side List <WebElement> a While this:

drive.driver.getTitle(By.xpath("somelocator"));

returns a single WebElement object instance so assigning a single object to a list

List <WebElement> a = drive.driver.findElement(By.xpath("somelocator"));

Will work, but will not really make sense.
Instead of that you should use something like

WebElement a = drive.driver.findElement(By.xpath("somelocator"));

While
String <WebElement> or Boolean <WebElement> a or Map <WebElement> a are simply illegal expressions.
String and Boolean are not valid collections.
Map will have to be defined for 2 object types: the key and the value, something like Map <WebElement,Integer> map while in order to add an entry to such map a you will need to write something like this:

Map <WebElement, Integer> map = new HashMap<>();
WebElement a = drive.driver.findElement(By.xpath("somelocator"));
map.put(a,1);
  •  Tags:  
  • Related