当前位置:   article > 正文

Selenium-12306自动抢票_12306抢票java

12306抢票java

源码: 

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By # driver.find_element(By.ID,'query_ticket')
  3. from selenium.webdriver.support.wait import WebDriverWait # 显示等待
  4. from selenium.webdriver import ActionChains # 动作链
  5. from selenium.webdriver import ChromeOptions # 规避检测
  6. import time
  7. # def login():
  8. # """
  9. # 判断网页url是否改变:
  10. # 当验证登录成功后,网页url会改变,即表示登录成功!
  11. # :return:
  12. # """
  13. # while True:
  14. # if driver.current_url != url:
  15. # print("用户登录成功!")
  16. # break
  17. def login():
  18. driver.find_element(By.ID, 'J-userName').send_keys('输入12306账号')
  19. driver.find_element(By.ID, 'J-password').send_keys('输入12306密码')
  20. driver.find_element(By.ID, 'J-login').click()
  21. time.sleep(2)
  22. # 滑动模块
  23. clock = driver.find_element(By.CLASS_NAME, 'nc_iconfont')
  24. action = ActionChains(driver)
  25. # 点击长按滑动模块
  26. action.click_and_hold(clock).perform()
  27. # for i in range(5):
  28. action.move_by_offset(300, 0)
  29. time.sleep(0.1)
  30. action.release().perform()
  31. def buy():
  32. """
  33. :return:
  34. """
  35. login()
  36. # 将 出发地 目的地 出发日期 -- 添加到cookie
  37. driver.add_cookie({'name': '_jc_save_fromDate', 'value': '2022-04-01'})
  38. driver.add_cookie({'name': '_jc_save_fromStation', 'value': '%u5E7F%u5DDE%u4E1C%2CGGQ'})
  39. driver.add_cookie({'name': '_jc_save_toStation', 'value': '%u6F6E%u9633%2CCNQ'})
  40. # 显示等待
  41. # 定位链接文字点击确定
  42. WebDriverWait(driver, 30).until(lambda x: x.find_element_by_link_text("确定")).click()
  43. WebDriverWait(driver, 30).until(lambda x: x.find_element_by_link_text("车票")).click()
  44. WebDriverWait(driver, 30).until(lambda x: x.find_element_by_link_text("单程")).click()
  45. # 点击查询
  46. driver.find_element(By.ID, 'query_ticket').click()
  47. # 获取当前页面(判断)
  48. url = driver.current_url
  49. while True:
  50. # 抢票
  51. if url != driver.current_url:
  52. print('查询成功!')
  53. break
  54. else:
  55. try:
  56. for i in driver.find_elements(By.CLASS_NAME, "btn72"): # 注:elements
  57. i.click()
  58. except:
  59. print("正在查询...") # 无预票避免错误跳出
  60. # 选择乘车人
  61. print('请选择乘车人')
  62. driver.find_element(By.ID, "normalPassenger_0").click()
  63. # 提交订单
  64. driver.find_element(By.LINK_TEXT, "提交订单").click()
  65. print('订单提交...')
  66. time.sleep(3)
  67. driver.find_element(By.LINK_TEXT, "确认").click()
  68. print('下单成功!待支付...')
  69. if __name__ == '__main__':
  70. # 启动浏览器
  71. url = "https://kyfw.12306.cn/otn/resources/login.html"
  72. # 设置浏览器,防止selenium被检测出来
  73. options = ChromeOptions()
  74. options.add_argument("--disable-blink-features=AutomationControlled")
  75. options.add_experimental_option('excludeSwitches', ['enable-automation'])
  76. driver = webdriver.Chrome(options=options)
  77. driver.get(url)
  78. buy()

模拟滑动验证码:

  1. # 滑动模块
  2. clock = driver.find_element(By.CLASS_NAME, 'nc_iconfont')
  3. action = ActionChains(driver)
  4. # 点击长按滑动模块
  5. action.click_and_hold(clock).perform()
  6. # for i in range(5):
  7. action.move_by_offset(300, 0)
  8. time.sleep(0.1)
  9. action.release().perform()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/632053
推荐阅读
相关标签
  

闽ICP备14008679号