Home > database >  Using arguments in Selenium Java on Xpath Expression
Using arguments in Selenium Java on Xpath Expression

Time:05-26

I want to use the argument 'option' as part of the findElement by the Xpath Expression:

 public HomePage clickSizeOption (String option){
        driver.findElement(By.xpath("span[contains(@class, 'checkmark') and text()= option]")).click();
        return this;
    }

But java treats the entire by xpath as a string.

HTML Context:

I need to select XS only but that would have to be from the argument

CodePudding user response:

You should be able to use the format() method for this. I also noticed your xpath doesn't start with '//'.
Afaik an xpath should always start with '//' except if you start from the html element.

Try this out

driver.findElement(By.xpath(String.format("//span[contains(@class, 'checkmark') and text()= %s]", option)).click();
  • Related