我想编写java selenium代码,用于从datepicker

时间:2016-02-03 17:06:55

标签: java selenium xpath css-selectors

@Given("^I choose destination to \"(.*?)\" on \"(.*?)\" from \"(.*?)\"  
with \"(.*?)\" or \"(.*?)\" or \"(.*?)\"$")
public void i_choose_destination_to_on_from_with_or_or(String where, String When, String departingfrom, String adults, String children, String infants) throws Throwable {
    driver.get("http://www.example.co.uk/");
    WebElement selectElement = driver.findElement(By.tagName("select"));
    Select selectObject = new Select(selectElement);
    selectObject.selectByVisibleText(where);
    Thread.sleep(200);
    driver.findElement(By.xpath("//div[@class='fieldcalendar']//input[@class='datepicker']")).click();
    driver.findElement(By.xpath("//div[@class='pika-lendar']//select[@class='pika-select pika-select-month']")).sendKeys("March");
    WebElement selectYearElement = driver.findElement(By.xpath("//select[@class='pika-select pika-select-year']"));
    Select selectYearObject = new Select(selectYearElement);
    selectYearObject.selectByValue("2016");
    List<WebElement> dayField = driver.findElements(By.cssSelector("button.pika-button.pika-day"));
    for (WebElement cell: dayField) {
        String tag = cell.getAttribute("data-pika-day");
        if (tag.equals("6")) {
            WebElement input = (new WebDriverWait(driver,15)).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("button.pika-button.pika-day")));
            //below code not working
            input.findElement(By.cssSelector("button.pika-button.pika-day")).click();
            break;
        }
    }
}

请参考这个网站。我尝试使用以下代码,但事实并非如此      工作点击日历中的日期。      这个月和年都很好。      需要帮助来解决它。欢迎任何建议

1 个答案:

答案 0 :(得分:0)

您正尝试从此处找到此按钮。您已在input变量中使用此按钮,但您可以使用它再次找到它。您只能通过这种方式查找子元素。只需做

input.click();

或者

(new WebDriverWait(driver,15)).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("button.pika-button.pika-day"))).click();
  

修改

ElementNotVisibleException表示您尝试点击的按钮不可见。尝试在预期条件中使用visibilityOfElementLocated

(new WebDriverWait(driver,15)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button.pika-button.pika-day"))).click();

如果您需要滚动到该元素以使其可见,您可以使用Actions

WebElement input = driver.findElement(By.cssSelector("button.pika-button.pika-day"));

Actions actions = new Actions(driver);
actions.moveToElement(input).build().perform(); // this will simulate scroll to the button

(new WebDriverWait(driver,15)).until(ExpectedConditions.visibilityOf(input)).click();