使用selenium Webdriver在页面上选择日期

时间:2016-05-12 07:18:15

标签: selenium testing selenium-webdriver gui-testing

这是我正在测试的网站https://www.sydneyairport.com.au/go/car-parking.aspx,我差不多完成了,但一直陷入困境。

我选择了"日期和时间"从输入日期部分,但不能选择"日期和时间"从退出日期部分。

我无能为力,为什么我无法做到这一点,因为两者的结构相同,而且我能够进入日期,我不明白退出日期部分的变化。我是硒的新手,如果有人帮助我,我会很感激。

这是我在入境日期部分选择日期和时间所写的内容。

 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;

    }

1 个答案:

答案 0 :(得分:1)

我为这段代码做了很多工作,你正在使用tr和td。我建议没有必要。

查看我使用的代码,我可以轻松选择两个日期。希望这能帮到你..

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();

请回复,如何找到这个并回复给我进一步查询。 快乐学习。 : - )