Reputation: 13
```
if(title.equalsIgnoreCase("Mr"))
{
driv.mr.click();
}
else if(title.equalsIgnoreCase("Mrs"))
{
driv.mrs.click();
}
else
{
driv.miss.click();
}
```
#object.java#
@FindBy(how = How.XPATH, using = "//label[text()='Mr']" )
public WebElement mr;
@FindBy(how = How.XPATH, using = "//label[text()='Mrs']" )
public WebElement mrs;
@FindBy(how = How.XPATH, using = "//label[text()='Miss']" )
public WebElement miss;
Here instead of passing 3 paths, I need to pass single XPath.Is there any way to do that???
Upvotes: 1
Views: 480
Reputation: 340
Just use String.format()
So you want something like this:
String selector = "//label[text()='%s']";
Then you could just do WebElement mr = driver.findElement(By.cssSelector(String.format(selector, "Mr")));
format will automatically take the content of the second argument, and put it in place of the %s.
Upvotes: 0
Reputation: 7708
Use |
in xpath to perform OR operation e.g.
@FindBy(how = How.XPATH, using = "//label[text()='Mr'] | //label[text()='Mrs'] | //label[text()='Miss']" )
public WebElement prefix;
This will return your element if any condition satisfy out of 3.
Upvotes: 0
Reputation: 1868
You can create shuch kind of method, and pass argument text to click on the needed element
public void clickOnButton(String text){
WebElement button = driver.findElement(By.xpath("//label[text() = '"+text+"']"));
button.click();
}
Upvotes: 1