使用 selenium Webdriver 在页面上选择日期
Selecting Date on a Page using selenium Webdriver
这是我正在测试的网站https://www.sydneyairport.com.au/go/car-parking.aspx,我快完成了,但一直卡在一个问题中。
我已经select编辑了"date and time"进入日期部分,但是不能select"date and time"退出日期部分。
我不知道为什么我不能这样做,因为两者的结构相同,而且我能够在进入日期做到这一点,我不明白退出日期部分有什么变化。我是 selenium 的新手,如果有人帮助我,我将不胜感激。
这是我在输入日期部分写入 select 日期和时间的内容。
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。我建议没有必要。
查看我正在使用的代码,我可以 select 轻松约会。希望对你有帮助..
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();
请回复,你是怎么找到这个的,然后回复我进一步查询。
快乐学习。 :-)
这是我正在测试的网站https://www.sydneyairport.com.au/go/car-parking.aspx,我快完成了,但一直卡在一个问题中。
我已经select编辑了"date and time"进入日期部分,但是不能select"date and time"退出日期部分。
我不知道为什么我不能这样做,因为两者的结构相同,而且我能够在进入日期做到这一点,我不明白退出日期部分有什么变化。我是 selenium 的新手,如果有人帮助我,我将不胜感激。
这是我在输入日期部分写入 select 日期和时间的内容。
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。我建议没有必要。
查看我正在使用的代码,我可以 select 轻松约会。希望对你有帮助..
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();
请回复,你是怎么找到这个的,然后回复我进一步查询。 快乐学习。 :-)