Javascript Selenium IDE:如何从datepicker中选择下一个可用日期
我希望我的selenium IDE测试用例按照以下步骤运行,以便自动选择日期:Javascript Selenium IDE:如何从datepicker中选择下一个可用日期,javascript,html,selenium,automation,automated-tests,Javascript,Html,Selenium,Automation,Automated Tests,我希望我的selenium IDE测试用例按照以下步骤运行,以便自动选择日期: 单击“出发日期”以打开日期选择器 从当前选择的日期开始,循环日期,直到到达下一个可用日期(如果需要,移动到下一个月或下一年以查找下一个可用日期) 从日期选择器中选择可用日期 有人能告诉我,我是硒的新手,如何在上面的例子中做到这一点吗?目前我的脚本所能做的就是打开日历 下面是我设法收到的与上面截图匹配的html: //Months drop down <select class="ui-datepic
- 单击“出发日期”以打开日期选择器
- 从当前选择的日期开始,循环日期,直到到达下一个可用日期(如果需要,移动到下一个月或下一年以查找下一个可用日期)
- 从日期选择器中选择可用日期
//Months drop down
<select class="ui-datepicker-month" data-handler="selectMonth" data-event="change">
<option value="2" selected="selected">Mar
</option><option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
</select>
//Years drop down
<select class="ui-datepicker-year" data-handler="selectYear" data-event="change">
<option value="2016" selected="selected">2016</option>
</select>
<table class="ui-datepicker-calendar">
//days labels
<thead>
<tr>
<th scope="col"><span title="Monday">Mo</span></th>
<th scope="col"><span title="Tuesday">Tu</span></th>
<th scope="col"><span title="Wednesday">We</span></th>
<th scope="col"><span title="Thursday">Th</span></th>
<th scope="col"><span title="Friday">Fr</span></th>
<th scope="col" class="ui-datepicker-week-end"><span title="Saturday">Sa</span></th>
<th scope="col" class="ui-datepicker-week-end"><span title="Sunday">Su</span></th>
</tr>
</thead>
<tbody>
//dates
<tr>
<td class=" ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled"> </td>
<td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">1</span></td>
<td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">2</span></td>
<td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">3</span></td>
<td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">4</span></td>
<td class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">5</span></td>
<td class=" ui-datepicker-week-end ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">6</span></td></tr>
<tr>
<td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">7</span></td>
<td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">8</span></td>
...same process till last week of dates (bottom row of calendar in screenshot)
<tr>
<td class=" ui-datepicker-days-cell-over ui-datepicker-current-day" title="Click to see flights on this date" data-handler="selectDay" data-event="click" data-month="2" data-year="2016"><a class="ui-state-default ui-state-active" href="#">28</a></td>
<td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">29</span></td>
<td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">30</span></td>
<td class=" ui-datepicker-unselectable ui-state-disabled " title="No available flights on this date"><span class="ui-state-default">31</span></td>
<td class=" ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled"> </td><td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled"> </td>
<td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled"> </td>
</tr>
</tbody>
</table>
//月份下拉列表
破坏
四月
也许
六月
七月
八月
九月
十月
//年份下降
2016
//天数标签
钼
屠
我们
Th
Fr
Sa
苏
//日期
1.
2.
3.
4.
5.
6.
7.
8.
…直到日期的最后一周都是相同的过程(屏幕截图中日历的底行)
29
30
31
在SeleIDE中实现这样一个任务将是非常有挑战性的,因为在获得下一个可用的日期时涉及到一个非平凡的逻辑,您应该考虑选择一个可用的硒语言绑定。
下面是一个工作代码。其想法是:
- 实例化WebDriver(在本例中使用
Firefox()
,但也有其他选择)
- 最大化浏览器窗口
- 导航到URL()
- 等待页面被显式加载()
- 填写出发和目的地字段
- 单击“离开”字段以触发要显示的日历
- 在日历中,找到当前日期-它具有
ui datepicker current day
类
- 使用“by XPath”定位技术和方法检查本月是否有可用日期。如果是,请打印出来并单击
- 如果我们没有找到本月的下一个可用日期,请初始化并“无止境”循环,然后单击“下一个”月按钮检查是否存在可用日期。打印出来,如果找到,单击并退出循环。否则,单击“下一步”按钮
- 如果我们没有“下一步”按钮,那么我们在一年结束时,在“年”下拉列表中选择下一年,继续循环
- 完成后关闭驱动程序
守则:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
FROM = "Leeds Bradford"
TO = "Budapest BUD"
driver = webdriver.Firefox() # or, webdriver.Chrome(), or webdriver.PhantomJS() or etc.
driver.maximize_window()
driver.get("http://www.jet2.com")
wait = WebDriverWait(driver, 10)
actions = ActionChains(driver)
# wait for the page to load
wait.until(EC.presence_of_element_located((By.ID, "departure-airport-input")))
# fill out the form
driver.find_element_by_id("departure-airport-input").send_keys(FROM)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ui-id-1 .ui-menu-item"))).click()
driver.find_element_by_id("destination-airport-input").send_keys(TO)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "#ui-id-2 .ui-menu-item"))).click()
# select date
datepicker = driver.find_element_by_id("departure-date-selector")
actions.move_to_element(datepicker).click().perform()
# find the calendar, month and year picker and the current date
calendar = driver.find_element_by_id("departureDateContainer")
month_picker = Select(calendar.find_element_by_class_name("ui-datepicker-month"))
year_picker = Select(calendar.find_element_by_class_name("ui-datepicker-year"))
current_date = calendar.find_element_by_class_name("ui-datepicker-current-day")
# printing out current date
month = month_picker.first_selected_option.text
year = year_picker.first_selected_option.text
print("Current date: {day} {month} {year}".format(day=current_date.text, month=month, year=year))
try:
# see if we have an available date in this month
next_available_date = current_date.find_element_by_xpath("following::td[@data-handler='selectDay' and ancestor::div/@id='departureDateContainer']")
print("Found an available date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
next_available_date.click()
except NoSuchElementException:
# looping over until the next available date found
while True:
# click next, if not found, select the next year
try:
calendar.find_element_by_class_name("ui-datepicker-next").click()
except NoSuchElementException:
# select next year
year = Select(calendar.find_element_by_class_name("ui-datepicker-year"))
year.select_by_visible_text(str(int(year.first_selected_option.text) + 1))
# reporting current processed month and year
month = Select(calendar.find_element_by_class_name("ui-datepicker-month")).first_selected_option.text
year = Select(calendar.find_element_by_class_name("ui-datepicker-year")).first_selected_option.text
print("Processing {month} {year}".format(month=month, year=year))
try:
next_available_date = calendar.find_element_by_xpath(".//td[@data-handler='selectDay']")
print("Found an available date: {day} {month} {year}".format(day=next_available_date.text, month=month, year=year))
next_available_date.click()
break
except NoSuchElementException:
continue
driver.close()
测试结果:
- 利兹-布拉德福德->安塔利亚AYT(下一个可用日期为4月):
- 利兹-布拉德福德->布达佩斯巴德(与当前日期同月的下一个可用日期):
- ??(下一年的下一个可用日期)
这是Java版本
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DatePicketJet2 {
public static WebDriver browser;
public static void main(String args[]) throws InterruptedException {
browser = new FirefoxDriver();
String url = "http://www.jet2.com/";
browser.get(url);
browser.manage().window().maximize();
browser.findElement(By.id("departureAirportList")).click();
WebElement departureCountry = browser.findElement(By.id("destinations-uk"));
WebElement departureCityLocator = departureCountry.findElement(By.tagName("ul"));
List<WebElement> departureCities = departureCityLocator.findElements(By.tagName("li"));
/*
for (WebElement we : departureCities) {
System.out.println(we.getText());
}
*/
departureCities.get(1).click();
browser.findElement(By.id("destinationAirportList")).click();
WebElement destinationCountry = browser.findElement(By.id("destinations-country"));
WebElement destinationCityLocator = destinationCountry.findElement(By.tagName("ul"));
List<WebElement> destinationCities = destinationCityLocator.findElements(By.tagName("li"));
/*
for (WebElement we : destinationCities) {
System.out.println(we.getText());
}
*/
destinationCities.get(1).click();
browser.findElement(By.id("departure-date-selector")).click();
WebElement departureMonth = browser.findElement(By.tagName("tbody"));
//System.out.println(departureMonth.getText());
List<WebElement> departureDate = departureMonth.findElements(By.tagName("a"));
for (WebElement we : departureDate) {
System.out.println("~" + we.getText());
we.click();
break;
}
browser.findElement(By.id("return-date-selector")).click();
Thread.sleep(1000);
WebElement returnMonth = browser.findElement(By.tagName("tbody"));
System.out.println("<>" + returnMonth.isEnabled());
System.out.println(returnMonth.getText());
List<WebElement> returnDate = returnMonth.findElements(By.tagName("a"));
for (WebElement we1 : returnDate) {
System.out.println("~" + we1.getText());
we1.click();
break;
}
Thread.sleep(1000);
browser.close();
}
}
import java.util.List;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
公共类DatePicketJet2{
公共静态WebDriver浏览器;
公共静态void main(字符串args[])引发InterruptedException{
浏览器=新的FirefoxDriver();
字符串url=”http://www.jet2.com/";
browser.get(url);
browser.manage().window().maximize();
browser.findElement(By.id(“departureAirportList”)。单击();
WebElement departureCountry=browser.findElement(By.id(“英国目的地”);
WebElement departureCityLocator=departureCountry.findElement(按.tagName(“ul”));
List departureCities=departureCityLocator.findElements(按.tagName(“li”));
/*
for(WebElement we:departureCities){
System.out.println(we.getText());
}
*/
获取(1)。单击();
browser.findElement(By.id(“destinationAirportList”)。单击();
WebElement destinationCountry=browser.findElement(By.id(“目的地国家”);
WebElement destinationCityLocator=destinationCountry.findElement(按.tagName(“ul”));
List destinationCities=destinationCityLocator.findElements(按.tagName(“li”));
/*
for(WebElement-we:destinationCities){
System.out.println(we.getText());
}
*/
get(1).click();
browser.findElement(By.id(“出发日期选择器”))。单击();
WebElement departureMonth=browser.findElement(按.tagName(“tbody”));
//System.out.println(departureMonth.getText());
List departureDate=departureMonth.findElements(按.tagName(“a”));
对于(WebElement we:departureDate){
System.out.println(“~”+we.getText());
we.click();
打破
}
browser.findElement(By.id(“返回日期选择器”))。单击();
睡眠(1000);
WebElement returnMonth=browser.findElement(按.tagName(“tbody”);
System.out.println(“+returnMonth.isEnabled());
System.out.println(returnMonth.getText());
List returnDate=returnMonth.findElements(按.tagName(“a”));
for(WebElement we1:returnDate){
System.out.println(“~”+we1.getText());
we1.单击();
打破
}
睡眠(1000);
browser.close();
}
}
查看
并使用“a”标记名称查找所有可见日期,您将获得所有可用日期或显示为绿色的日期
稍后根据您的需求优化您的逻辑。通过Selenium IDE实现这一点非常具有挑战性。参考链接,了解gotoIF的工作原理。从下载用户扩展js并添加到selenium IDE。如果您可以使用Selenium Webdriver,那么您将很容易实现
我下面的例子是通过Selenium IDE实现的。我只举了一个选择出境航班的例子。在这两种情况下,我首先发送带有今天日期的url作为出发和到达日期。然后我使用Current date: 12 Feb 2016
Found an available date: 15 Feb 2016
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DatePicketJet2 {
public static WebDriver browser;
public static void main(String args[]) throws InterruptedException {
browser = new FirefoxDriver();
String url = "http://www.jet2.com/";
browser.get(url);
browser.manage().window().maximize();
browser.findElement(By.id("departureAirportList")).click();
WebElement departureCountry = browser.findElement(By.id("destinations-uk"));
WebElement departureCityLocator = departureCountry.findElement(By.tagName("ul"));
List<WebElement> departureCities = departureCityLocator.findElements(By.tagName("li"));
/*
for (WebElement we : departureCities) {
System.out.println(we.getText());
}
*/
departureCities.get(1).click();
browser.findElement(By.id("destinationAirportList")).click();
WebElement destinationCountry = browser.findElement(By.id("destinations-country"));
WebElement destinationCityLocator = destinationCountry.findElement(By.tagName("ul"));
List<WebElement> destinationCities = destinationCityLocator.findElements(By.tagName("li"));
/*
for (WebElement we : destinationCities) {
System.out.println(we.getText());
}
*/
destinationCities.get(1).click();
browser.findElement(By.id("departure-date-selector")).click();
WebElement departureMonth = browser.findElement(By.tagName("tbody"));
//System.out.println(departureMonth.getText());
List<WebElement> departureDate = departureMonth.findElements(By.tagName("a"));
for (WebElement we : departureDate) {
System.out.println("~" + we.getText());
we.click();
break;
}
browser.findElement(By.id("return-date-selector")).click();
Thread.sleep(1000);
WebElement returnMonth = browser.findElement(By.tagName("tbody"));
System.out.println("<>" + returnMonth.isEnabled());
System.out.println(returnMonth.getText());
List<WebElement> returnDate = returnMonth.findElements(By.tagName("a"));
for (WebElement we1 : returnDate) {
System.out.println("~" + we1.getText());
we1.click();
break;
}
Thread.sleep(1000);
browser.close();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://www.jet2.com/" />
<title>21jet2_01</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">21jet2_01</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/cheap-flights/leeds-bradford/larnaca/2016-02-07/2016-02-07?adults=2</td>
<td></td>
</tr>
<tr>
<td>storeElementPresent</td>
<td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, "LS1FlightDay flights")]/a/div/span[@class='fare']</td>
<td>x</td>
</tr>
<tr>
<td>gotoIf</td>
<td>${x} == true</td>
<td>select</td>
</tr>
<tr>
<td>label</td>
<td>clickNextMonth</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>xpath=//div[@class='monthview outbound ']/div/div[2]/a[@class='changepage nextmonth icon icon-arrow-next']</td>
<td></td>
</tr>
<tr>
<td>storeElementPresent</td>
<td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, "LS1FlightDay flights")]/a/div/span[@class='fare']</td>
<td>y</td>
</tr>
<tr>
<td>gotoIf</td>
<td>${y} == false</td>
<td>clickNextMonth</td>
</tr>
<tr>
<td>label</td>
<td>select</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, "LS1FlightDay flights")]/a/div/span[@class='fare']</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="http://www.jet2.com/" />
<title>21jet2_01</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">21jet2_01</td></tr>
</thead><tbody>
<tr>
<td>open</td>
<td>/cheap-flights/belfast/alicante/2016-02-07/2016-02-07?adults=2</td>
<td></td>
</tr>
<tr>
<td>storeElementPresent</td>
<td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, "LS1FlightDay flights")]/a/div/span[@class='fare']</td>
<td>x</td>
</tr>
<tr>
<td>gotoIf</td>
<td>${x} == true</td>
<td>select</td>
</tr>
<tr>
<td>label</td>
<td>clickNextMonth</td>
<td></td>
</tr>
<tr>
<td>clickAndWait</td>
<td>xpath=//div[@class='monthview outbound ']/div/div[2]/a[@class='changepage nextmonth icon icon-arrow-next']</td>
<td></td>
</tr>
<tr>
<td>storeElementPresent</td>
<td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, "LS1FlightDay flights")]/a/div/span[@class='fare']</td>
<td>y</td>
</tr>
<tr>
<td>gotoIf</td>
<td>${y} == false</td>
<td>clickNextMonth</td>
</tr>
<tr>
<td>label</td>
<td>select</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>xpath=//div[@class='monthview outbound ']/div[2]/table/tbody/tr/td[starts-with(@class, "LS1FlightDay flights")]/a/div/span[@class='fare']</td>
<td></td>
</tr>
</tbody></table>
</body>
</html>