繁体   English   中英

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

[英]Selecting Date on a Page using selenium Webdriver

这是我正在测试的网站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;

    }

我为这段代码做了很多工作,你正在使用 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();

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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