我希望我的selenium IDE测试用例像下面的步骤一样运行,以便自动选择日期:

>单击出发日期以打开日期选择器
>从当前选定的日期开始,循环显示日期,直到到达下一个可用日期(如果需要,请移至下个月或下一年以查找下一个可用日期)
>从日期选择器中选择可用日期

有人可以告诉我,因为我对selenium不熟悉如何为上述例子做这件事吗?我的所有脚本目前都可以打开日历.

下面是我设法收到的与上面截图匹配的html:

//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">&nbsp;</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">&nbsp;</td><td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td>
    <td class=" ui-datepicker-week-end ui-datepicker-other-month ui-datepicker-unselectable ui-state-disabled">&nbsp;</td>
    </tr>
    </tbody>
    </table>

解决方法

在Selenium IDE中自动执行此类任务将非常具有挑战性,因为在获取下一个可用日期时涉及非平凡的逻辑,您应该考虑 switching to Selenium WebDriver选择一种可用的selenium语言绑定.

这是使用Python language Selenium bindings制作的工作代码.这个想法是:

>实例化WebDriver(在此示例中使用Firefox(),但也有其他选择)
>最大化浏览器窗口
>导航到URL(http://www.jet2.com)
>等到明确加载页面(docs)
>填写出发地和目的地
>单击“离开”字段以触发要显示的日历
>在日历中,找到当前日期 – 它具有ui-datepicker-current-day类
>使用“by XPath”定位技术和following axis检查本月是否有可用日期.如果是,请将其打印出来并单击.
>如果我们在本月没有找到下一个可用日期,请初始化并“无限”循环,然后单击“下一步”月份按钮,检查我们是否有可用日期.将其打印出来然后单击(如果找到)并退出循环.否则,单击“下一步”按钮.
>如果我们没有“下一步”按钮 – 那么我们在一年结束时,选择下一年的下拉菜单,继续循环
>完成后关闭驱动程序

代码:

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,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,year=year))
            next_available_date.click()
            break
        except NoSuchElementException:
            continue

driver.close()

检测结果:

>利兹布拉德福德 – >安塔利亚AYT(下一个4月可用日期):

Current date: 28 Mar 2016
Processing Apr 2016
Found an available date: 4 Apr 2016

>利兹布拉德福德 – > Budapest BUD(下一个可用日期与当前日期相同):

Current date: 12 Feb 2016
Found an available date: 15 Feb 2016

>? (下一年的下一个可用日期)

javascript – Selenium IDE:如何从datepicker中选择下一个可用日期的更多相关文章

  1. ios – DatePicker包含Year

    我在我的应用程序中实现了dataPicker.但它只显示月,日,小时.我怎么能在日期选择器中添加年份.任何的想法?

  2. 适用于iOS和Android设备的自动验收测试

    我正在开展一个网络项目,很明显,进行一些Selenium类型的自动验收测试会非常有帮助.但该项目是移动/手持设备的网站,而非Selenium支持的桌面浏览器.谷歌搜索一下,发现iOS为Frank,Android为Robotium.这些是使用的两种工具吗?或者有什么好的或更好的东西可能允许我使用Android和iOS的一个工具?不确定这对于移动设备上的Web测试有何用处.

  3. iOS 5 – 将UIDatePicker最小日期设置为“今天”

    我知道如何将UIDatePicker中的最大和最小日期设置为某个日期,我想知道是否可以这样做,所以最小日期始终是“今天”日期.在我的应用程序中,用户输入过去的日期是没有意义的,只是为了让他们添加一个今天或将来的日期.解决方法将最短日期设置为现在,将显示日期设置为2秒:在初始化Nib或在重新使用时为视图控制器设置数据时执行此操作.您也可以在viewWillAppear中执行此操作.额外积分:您可以添加一种IBAction方法,巧妙地鼓励用户不要选择错误的建议日期:

  4. ios – 在greift中将gregorian datepicker转换为波斯语日期选择器?

    解决方法您需要设置日历,而不是dateFormat:如果要更改语言,还必须设置语言环境属性:此外,正如问题注释所述,您正在使用NSDateFormatter,而不是代码中的UIDatePicker.幸运的是,答案仍然是正确的;UIDatePicker还有一个日历属性,您可以设置该属性来实现此目标.

  5. ios – 如果用户以编程方式命中UITextfield,如何显示UIDatePicker

    只有当用户UITextField被点击时,才想显示UIDatePicker.当选择日期时,它应该显示在相同的UITextField中.我想以编程方式实现UIDatePicker.我知道如何编写UITextField的代码.我甚至不知道如何调用UIDatePicker.有人可以帮我吗出生日期解决方法最简单的方法是实现datePicker如下:创建datePicker:然后将datePicker链接到textfieldinputView:最后,您可以在选择日期时捕获该操作,并将日期转换为字符串,以在textF

  6. ios – 用UIDatePicker替换UITextField输入

    ,我有一个很大的例外–我不能让选择器消失!我尝试注册事件UIControlEventTouchUpOutside,但似乎这不是由选择器生成的.如何识别用户已完成选择日期?还有,有没有办法禁止用户直接输入UITextField?我想强迫他们使用选择器.我看到这篇文章“DisableblinkingcursorinUITextField?”,但还有另一种方式吗?

  7. 如何在iOS中使用DatePicker仅接受月份和年份

    我正在使用swift构建应用程序,在该应用程序中,有一个字段用于接受借记卡的到期日期以进行付款.如何在Swift中使用DatePicker只接受月份和年份.如果使用datepicker是不可能的,那么请建议任何其他方式.先感谢您.解决方法选择-1Objective-C的迅速Swift3另一种模式是>UIDatePickerModeTime,>UIDatePickerModeDate,>UIDate

  8. ios – 将UIDatePicker添加到UIAlertView

    我正在尝试将日期选择器添加到警报视图.我可以根据按钮点击将动画添加到屏幕上,我看到一个黑盒但没有日期选择器.这是我到目前为止所拥有的……

  9. swift UI专项训练28 DatePicker日期老虎机

    如果你设置过iPhone中的日期,那么你对日期老虎机一定不陌生。主要属性有日期、时间和倒计时,如图:mode模式中你可以选择只有日期、只有时间或者都有。后面的是一些约束。Timer是倒计时间隔。拖一个到storyboard中,运行一下看看效果:还是满炫酷的。现在我们把它的样式设为倒计时,cutdowntimer:可以看到倒计时样式是没有我们常用的秒或者毫秒倒计时的,需要我们继承基类自己定义。

  10. Swift - 动态添加删除TableView的单元格以及内部元件-日期控件

    比如我们做一个消息提醒页面,默认页面只显示两个单元格。当点击第二个单元格时,下面会再添加一个单元格放置日期选择控件。而再次点击第二个单元格,日期选择控件又会隐藏。//日期选择器显示状态datePickerVisible:Bool=falseoverridefuncviewDidLoad(){super.viewDidLoad()self.title="添加任务"//去除尾部多余的空行.tableView.tableFooterView=UIView}didReceiveMemoryWarning(){.d

随机推荐

  1. js中‘!.’是什么意思

  2. Vue如何指定不编译的文件夹和favicon.ico

    这篇文章主要介绍了Vue如何指定不编译的文件夹和favicon.ico,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  3. 基于JavaScript编写一个图片转PDF转换器

    本文为大家介绍了一个简单的 JavaScript 项目,可以将图片转换为 PDF 文件。你可以从本地选择任何一张图片,只需点击一下即可将其转换为 PDF 文件,感兴趣的可以动手尝试一下

  4. jquery点赞功能实现代码 点个赞吧!

    点赞功能很多地方都会出现,如何实现爱心点赞功能,这篇文章主要为大家详细介绍了jquery点赞功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  5. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  6. JavaScript面向对象编程入门教程

    这篇文章主要介绍了JavaScript面向对象编程的相关概念,例如类、对象、属性、方法等面向对象的术语,并以实例讲解各种术语的使用,非常好的一篇面向对象入门教程,其它语言也可以参考哦

  7. jQuery中的通配符选择器使用总结

    通配符在控制input标签时相当好用,这里简单进行了jQuery中的通配符选择器使用总结,需要的朋友可以参考下

  8. javascript 动态调整图片尺寸实现代码

    在自己的网站上更新文章时一个比较常见的问题是:文章插图太宽,使整个网页都变形了。如果对每个插图都先进行缩放再插入的话,太麻烦了。

  9. jquery ajaxfileupload异步上传插件

    这篇文章主要为大家详细介绍了jquery ajaxfileupload异步上传插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. React学习之受控组件与数据共享实例分析

    这篇文章主要介绍了React学习之受控组件与数据共享,结合实例形式分析了React受控组件与组件间数据共享相关原理与使用技巧,需要的朋友可以参考下

返回
顶部