rocky
rocky

Reputation: 11

How to print two webelement in single print?

Here is my code:

WebElement firstname = driver.findElement(By.name("firstname"));

firstname.sendKeys("X");

WebElement lastname = driver.findElement(By.name("lastname"));

lastname.sendKeys("Y");

System.out.println(firstname lastname); // ???

Upvotes: 1

Views: 83

Answers (4)

Anubhav Mishra
Anubhav Mishra

Reputation: 106

If you want to print two WebElement you can use your code in the below format:

WebElement firstname = driver.findElement(By.name("firstname"));

firstname.sendKeys("X");

WebElement lastname = driver.findElement(By.name("lastname"));

lastname.sendKeys("Y");

System.out.println(firstname + "--------" + lastname);

However you will get only nonsense value which is of no use. Your output will be the String representation of WebElement. Can you please let me know what you are trying to achieve here so that I will be able to help you in a better way.

Upvotes: 0

Pavel Janicek
Pavel Janicek

Reputation: 14738

I guess you are trying to achieve this:

String firstNametoPrint = firstname.getAttribute("value");
String lastNametoPrint = lastname.getAttribute("value");
System.out.println(firstNametoPrint+"\n"+lastNametoPrint);

Upvotes: 4

Arif Acar
Arif Acar

Reputation: 1571

System.out.println(String.format("Name %s, Surname %s",firstname, lastname));

Upvotes: 0

optimistic_creeper
optimistic_creeper

Reputation: 2799

Do the following:

System.out.println(firstname+"\n"+lastname);

Upvotes: 1

Related Questions