直接上代码
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import time
value_fromstation = '%u5357%u5B81%2CNNZ' # 始发站(南宁)
value_tostation = '%u6B66%u6C49%2CWHN' # 终点站(武汉)
value_date = '2019-01-29' # 出发时间
def login_proc(username, password):
# 打开登录页面
sel = webdriver.Chrome(r'C:\Python\Scripts\chromedriver')#启动chromedriver的文件的位置
# sel=webdriver.Firefox()
sel.implicitly_wait(30)
login_url = 'https://kyfw.12306.cn/otn/login/init'
sel.get(login_url)
# sign in the username
try:
user_input = sel.find_element_by_id("username")
user_input.clear()
user_input.send_keys(username)
print('user-id write success!')
except:
print('user-id write error!')
# sign in the pasword
try:
pwd_input = sel.find_element_by_id("password")
pwd_input.clear()
pwd_input.send_keys(password)
print('pw write success!')
except:
print('pw write error!')
# Check for Login success
while True:
curpage_url = sel.current_url
if curpage_url != login_url:
if curpage_url[:-1] != login_url: # choose wrong verify_pic
print('Login finished!')
break
else:
time.sleep(5)
print(u'------------>等待用户图片验证')
return sel
def search_proc(sel, train_type='', timer=False):
print(u'----