• 读书笔记08携程项目实战之线性代码


    from datetime import datetime,date,timedelta
    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    import time
    #以下是定义函数部分, 其目的是返回今天后的第n天的日期,格式为2019-04-06
    def date_n(n):
    return str((date.today()+timedelta(days= +int(n))).strftime("%Y-%m-%d"))
    #以下变量用于定义搜索火车票的出发站和到达站
    from_station = "上海"
    to_station = "杭州"
    #以下为tomorrow变量
    tomorrow = date_n(1)
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get("https://trains.ctrip.com/")
    time.sleep(2)
    driver.find_element_by_name("departCityName").send_keys(from_station)
    time.sleep(2)
    driver.find_element_by_id("arriveCityName").send_keys(to_station)
    #移除出发时间的‘read-only'属性
    driver.execute_script("document.getElementById('departDate').removeAttribute('readonly')")
    time.sleep(2)
    #消除出发时间的默认内容
    driver.find_element_by_id('departDate').clear()
    time.sleep(2)
    #定义搜索车次日期
    driver.find_element_by_id('departDate').send_keys(tomorrow)
    #以下步骤是为了解决日期空间弹出窗在输入日期后无法消失的问题,以防影响测试的进行
    #原理是为了让鼠标左键单机页面空白处
    #ActionChains(driver).move_by_offset(1000,1000).click().perform()
    #单击车次搜索按钮
    driver.find_element_by_xpath('//input[@class="searchbtn"]').click()
    time.sleep(2)
    #通过在K1805车次的硬座区域单击“预定”按钮来余党车票
    driver.find_element_by_css_selector("body > div:nth-child(33) > div > div.lisBox > div.List-Box > div > div:nth-child(1) > div.w6 > div:nth-child(1) > a").click()
    time.sleep(3)
    #不登录携程系统订票
    driver.find_element_by_id("showCtrip").click()
    driver.find_element_by_id("btn_nologin").click()

    time.sleep(3)
    driver.find_element_by_css_selector("#inputPassengerVue > div.pasg-add > ul > li:nth-child(2) > input.input-name").send_keys("Jacqueline")

    接下来会进行重构和优化



  • 相关阅读:
    思考-少写代码
    app上传 那些事儿!
    vs2010 找不到本地服务器
    如何成为一名优秀得程序员
    python成功之路,Day2-判断和循环语句
    python成功之路,Day1-发展历史
    ES6学习笔记2-字符串扩展
    ES6学习笔记1-解构赋值
    数组的方法
    ES6
  • 原文地址:https://www.cnblogs.com/JacquelineQA/p/14811298.html
Copyright © 2020-2023  润新知