假设我有一个XPath来识别这样的元素:
//div[@id='%s1']//div[@class='%s2']
s1和s2的值将在运行时传递。是否可以通过从我的代码传递这两个变量来形成XPath(String)?
答案 0 :(得分:0)
public String varLocator(String locator, String holder1, String param1, String holder2, String param2) {
return locator.replace(holder1, param1).replace(holder2, param2);
}
public void sample() {
String eleLocator = "//div[@id='%s1']//div[@class='%s2']";
WebElement foo = driver.findElement(By.xpath(varLocator(eleLocator, "%s1", "foo", "%s2", "bar")));
}