简体   繁体   中英

Selecting Date on a Page using selenium Webdriver

This is the website I am testing https://www.sydneyairport.com.au/go/car-parking.aspx , I am almost done , but have been stuck in a single issue .

I have selected "date and time" from entry-date section but cannot select "date and time" from exit-date section.

I am clueless why I am not able to it since its the same structure for both and I was able to do it for entry-date ,I don't understand what changes for exit date section . I am new to selenium and will appreciate if anybody helps me out .

This is what I have written to select date and time in entry date section .

 public void selectDate(WebDriver driver, String fromDate, String toDate) {


            // selects from date 

            WebElement dateButton = driver.findElement(By.id("period_picker_0"));
            dateButton.click();
            WebElement datepicker = driver.findElement(By.xpath("//div[@class='period_picker_days']/table/tbody/tr/td[1]"));        

            selectDate(datepicker, fromDate);

            WebElement timeBox = driver.findElement(By.xpath("//div[@class='period_picker_work']/div[2]/input"));
            timeBox.sendKeys("");
            WebElement time = driver
                    .findElement(By.xpath(".//*[@id='timepicker_box_start']/div/div[2]/div/div[1]/div[13]"));
            time.click();


            // Selects to date 

            WebElement dateButton2 = driver.findElement(By.id("period_picker_1"));
            dateButton2.click();
            // dateButton.click();
            WebElement datepicker2 = driver
                    .findElement(By.xpath("//div[@class='period_picker_days']/table/tbody/tr/td[2]"));

            selectDate(datepicker2, toDate);

            WebElement timeBoxEnd = driver.findElement(By.xpath("//div[@class='period_picker_work']/div[2]/input"));
            timeBoxEnd.sendKeys("");
            WebElement timeEnd = driver
                    .findElement(By.xpath(".//*[@id='timepicker_box_end']/div/div[2]/div/div[1]/div[13]"));
            timeEnd.click();

        }

public int selectDate(WebElement datepicker, String date) {
        int ele = 0;
        List<WebElement> rows_table = datepicker.findElements(By.tagName("tr"));
        int rows_count = rows_table.size();
        for (int row = 0; row < rows_count; row++) {
            // To locate columns(cells) of that specific row.
            List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
            // To calculate no of columns(cells) In that specific row.
            int columns_count = Columns_row.size();
            // Loop will execute till the last cell of that specific row.
            for (int column = 0; column < columns_count; column++) {
                // To retrieve text from that specific cell.
                if (Columns_row.get(column).getText().equals(date)) {
                    ele = column;
                    Columns_row.get(column).click();
                }
            }
        }

        return ele;

    }

I did lot of work for this code, You are using tr and td. I would suggest there is no need of it.

See the code I'm using, i am able to select both date easily. Hope this help you..

driver.get("https://www.sydneyairport.com.au/go/car-parking.aspx");
driver.manage().window().maximize();
driver.findElement(By.xpath(".//*[@id='period_picker_0']")).click();

Actions a = new Actions(driver);

WebDriverWait wait = new WebDriverWait(driver,20);

WebElement entrydate= driver.findElement(By.xpath(".//*[@id='body']/div[1]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[5]/td[5]"));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='body']/div[1]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[5]/td[5]")));
a.moveToElement(entrydate).build().perform();
Thread.sleep(5000L);
entrydate.click();

WebElement entrytime= driver.findElement(By.xpath(".//*[@id='timepicker_box_start']/div/div[2]/div/div[1]/div[15]"));
a.moveToElement(entrytime).build().perform();
Thread.sleep(5000L);
entrytime.click();

WebElement exitdate= driver.findElement(By.xpath(".//*[@id='body']/div[2]/div[2]/div[1]/table/tbody/tr/td[1]/table/tbody/tr[6]/td[5]"));
a.moveToElement(exitdate).build().perform();
Thread.sleep(5000L);
exitdate.click();

WebElement exittime= driver.findElement(By.xpath(".//*[@id='timepicker_box_end']/div/div[2]/div/div[1]/div[15]"));
a.moveToElement(exittime).build().perform();
Thread.sleep(5000L);
exittime.click();

Please do reply, how you find this and reply back to me for further query. Happy Learning. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM