Selenium查找在Java中更改的xPath ID元素

时间:2017-06-05 20:40:09

标签: java regex selenium selenium-webdriver

我有这个xPath://*[@id="searchpopupSCH_USER_1496689382343"]

但在后端,13位数字实际上是一个时间戳,因此每次重新测试/重新点击时它都会改变。

例如,

1st tab, //*[@id="searchpopupSCH_USER_1496689382343"] 2nt tab, //*[@id="searchpopupSCH_USER_1496694868441"]

是否要定位不断变化的ID?

我正在考虑使用Reg Exp,但不知怎的,我有点阻止。

    String frameID = "//*[@id=\"searchpopupSCH_USER_";
    String regExp = "\\d{13}";

    Pattern p = Pattern.compile(frameID + regExp);

    WebElement xPathUserList;
    xPathUserList.findElement(By.xpath(p));  //param of xpath only take String 

我关心的是WebElement。

如何定位By.Path(" // * [@ id = \" searchpopupSCH_USER_xxxxxxxxxxxx"

感谢您的投入。

2 个答案:

答案 0 :(得分:0)

使用包含?甚至starts-with

 //*[contains(@id, "searchpopupSCH_USER_")]
 //*[starts-with(@id, "searchpopupSCH_USER_")]

答案 1 :(得分:-1)

声明如下变量:

public static final String TIME_STAMP = new SimpleDateFormat("ddMMyyHHmmss").format(Calendar.getInstance().getTime());

然后在xpath:

driver.findElement(By.xapth("//htmltag[@id="searchpopupSCH_USER_".concat(TIME_STAMP + "]"));

根据您的要求,您可以更改日期格式。

相关问题