无法在selenium datepicker值中打印验证

时间:2017-02-21 13:42:20

标签: selenium datepicker

我是selenium的新手,我正在尝试验证datepicker字段中的所选日期,但是当我执行此程序时,它正在打印其他部分,它不应该显示。请帮助知道我哪里出错了。

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class tripadviser {
    static WebDriver driver;
    public static void main(String[] args) {
        String start_date = "29-March 2017";
        String[] splitter = start_date.split("-");
        String start_day = splitter[0];
        String start_month = splitter[1];

        System.setProperty("webdriver.chrome.driver", "D:\\rakesh\\software\\selenium browser\\New folder\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.tripadvisor.in/");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        driver.findElement(By.id("rdoFlights")).click();

        WebElement datepicker_ele = driver.findElement(By.id("metaCheckInSpan"));
        datepicker_ele.click();

        select_date_fun(start_day, start_month);

        WebElement check_in_ele = driver.findElement(By.id("checkIn"));
        String datepicker_expected_str = "29/3/2017";
        if (check_in_ele.getText().equals(datepicker_expected_str)) {
            System.out.println("Date selection is done succesfully");
        } else {
            System.out.println("Something went wrong");
        }
        driver.close();
    }
    public static void select_date_fun(String day, String month_year) {
        List < WebElement > month_ele = driver.findElements(By.xpath(".//div[@class='calendar']/div[@class='month']/table/thead/tr/th[@class='caption']"));

        for (int i = 0; i < month_ele.size(); i++) {
            //System.out.println(month_ele.get(i).getText());
            if (month_ele.get(i).getText().equals(month_year)) {
                List < WebElement > day_ele = driver.findElements(By.xpath(".//div[@class='calendar']/div[@class='month'][1]/table/tbody/tr/td/a"));
                for (WebElement j: day_ele) {
                    //System.out.println(j.getText());
                    if (j.getText().equals(day)) {
                        j.click();
                        return;
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用.getAttribute(“value”)代替.getText()

来解决我的问题