读书笔记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")

接下来会进行重构和优化



原文地址:https://www.cnblogs.com/JacquelineQA/p/14811298.html