3

Can somebody please help me on the following?

I want to click on a button who has the text "Resume Scanning". In the below script. Instead of the hard coding (Resume Scanning), how can I pass the value from property file?

driver.findElement(by.xpath("//button[contains(.,'Resume Scanning')]"));

Thanks, Kannan C

k.s. Karthik
  • 731
  • 6
  • 16
Kannan C
  • 93
  • 2
  • 3
  • 9

4 Answers4

2

try as follows:

String label = "Resume Scanning";
driver.findElement(By.xpath("//button[contains(.,'" + label + "')]"));

Or using String.format:

String string = String.format("//button[contains(.,'%s')]", label);
driver.findElement(By.xpath(string));

Reference:

  1. Java - Including variables within strings?
Community
  • 1
  • 1
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • I used this one String label = "Resume Scanning"; driver.findElement(By.xpath("//button[contains(.,'" + label + "')]")); – Kannan C Nov 18 '16 at 11:36
0

You can use following java code to read from properties file

FileInputStream in = new FileInputStream("location of properties file");
Properties prop = new Properties();
prop.load(in);
String buttonText=prop.getProperty(propertyName);
driver.findElement(By.xpath(".//button[contains(text(),'"+buttonText+"')]"));
0

You can use a property file to read the text value. Here is an example:

inputParams.properties
----------------------
buttonLabel=Resume Scanning

Use the below sample code to read data from the property file:

FileReader reader=new FileReader("inputParams.properties");  
Properties p=new Properties();  
p.load(reader);  
System.out.println(p.getProperty("buttonLabel"));

Hope it helps.

k.s. Karthik
  • 731
  • 6
  • 16
0

By.xpath("//button[contains(.,'Add Strategy')]")

By.xpath("//button[contains(.,'Submit')]")

S. Abid
  • 13
  • 8
  • 1
    It may be helpful to provide some additional details, that explain what this code is doing, and how exactly this addresses the OP's question. – JonathanDavidArndt Aug 31 '18 at 11:42
  • Its is html base xpath finder when driver find any button which text is same with define text it will get access. – S. Abid Aug 31 '18 at 11:52