Jump to content

How can I have a variable with single quotes


newcoder1010
 Share

Recommended Posts

Hello,

//works
List textDemo = (List) driver.findElements(By.xpath("//*[contains(text(),'First Name is required.')]"));
//does not work
String name = "\'First Name is required.\'";
List textDemo = (List) driver.findElements(By.xpath("//*[contains(text(),name)]"));

First code has this 'First Name is required.' with single code inside double quotes.

Second code has a variable name which stores 'First Name is required.'. It is not working. How can I have name variable with single quotes 'First Name is required.'?

 

Link to comment
Share on other sites

// here it works just fine. If and else works as expected
List textDemo = (List) driver.findElements(By.xpath("//*[contains(text(),'First Name is required8.')]"));
if(((java.util.List<String>) textDemo).isEmpty())
{
System.out.println("Element using text is not found");
}
		 
else
System.out.println("Element using text is found");
}
// here only else block is being executed every time regardless I have correct value or not. 
String x = "\'First Name is required wrong value.\'"; // I also tried without the single quotes and escaped \ inside double quotes
System.out.println("x " + x);
List textDemo = (List) driver.findElements(By.xpath("//*[contains(text(),x)]"));
if(((java.util.List<String>) textDemo).isEmpty())
{
System.out.println("Element using text is not found");
}
else
System.out.println("Element using text is found");
}

This is selenium java webdriver/java related issues. I have problem only when I store the string in a variable. I am not sure how to fix it. 

Edited by newcoder1010
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...