错硒+点击+ <rich:calendar>

时间:2018-03-27 17:04:20

标签: selenium

我在点击组件中当天的元素时遇到问题。 Selenium通过xpath查找元素,发送click命令,但实际上没有单击该元素。代码和图像以便进一步理解。

值得一提的是,即使使用JavaScriptExecutor,仍然无法点击。

public static void selecionarDataCalendario(By by,WebDriver driver,WebDriverWait wait, WebElement webElement) throws Throwable {
    Calendar dat = Calendar.getInstance();
    dat.setTime(new Date());
    SimpleDateFormat formatterMes = new SimpleDateFormat("MM");
    SimpleDateFormat formatterAno = new SimpleDateFormat("YYYY");
    SimpleDateFormat formatterDia = new SimpleDateFormat("dd");
    String mes = formatterMes.format(dat.getTime());
    String ano = formatterAno.format(dat.getTime());
    String dia = formatterDia.format(dat.getTime());

    driver.findElement(by).click();
    Thread.sleep(500);
    driver.findElement(By.xpath("//td[text()='"+ dia +"']")).click();
    Thread.sleep(500);
}

Printscreen

1 个答案:

答案 0 :(得分:0)

我发现的解决方案是因为我通过文本搜索元素。迭代列表并单击所有找到的元素。

public static void selecionarDataCalendario(By by,WebDriver driver,WebDriverWait wait, WebElement webElement) throws Throwable {
    Calendar dat = Calendar.getInstance();
    dat.setTime(new Date());
    SimpleDateFormat formatterMes = new SimpleDateFormat("MM");
    SimpleDateFormat formatterAno = new SimpleDateFormat("YYYY");
    SimpleDateFormat formatterDia = new SimpleDateFormat("dd");
    String mes = formatterMes.format(dat.getTime());
    String ano = formatterAno.format(dat.getTime());
    String dia = formatterDia.format(dat.getTime());

    driver.findElement(by).click();
    Thread.sleep(500);
    ArrayList<WebElement> elementos = (ArrayList<WebElement>) driver.findElements(By.xpath("//table[@id='SalvarForm:dataefeito']/tbody/tr/td[text()='"+ dia +"']"));
    for (WebElement elemento: elementos) {
        elemento.click();
    }
    Thread.sleep(500);
}
相关问题