import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support import wait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class BuyTicket:
def __init__(self, driver, url, userName, password, go, to, start, end):
self.driver = driver
self.url = url
self.userName = userName
self.password = password
self.go = go
self.to = to
self.start = start
self.end = end
def login(self):
self.driver.get(self.url)
time.sleep(1)
self.driver.find_element(By.ID, "closeAdPop").click()
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@id="mobile-body"]/div[1]/div/div[3]/div[1]/span[2]/a[1]').click()
time.sleep(3)
handles = self.driver.window_handles
self.driver.switch_to.window(handles[-1])
self.driver.switch_to.frame("MxsLoginIframe")
self.driver.find_element(By.XPATH, '//*[@id="J_AccPwdLogin"]').click()
time.sleep(3)
self.driver.find_element(By.XPATH, '//*[@id="J_NormalLogin"]/div[1]/input[1]').send_keys('17837752383')
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@id="J_NormalLogin"]/div[2]/input').send_keys('password123')
time.sleep(1)
self.driver.find_element(By.ID, 'isAgreeSpringUserRule').click()
time.sleep(1)
self.driver.find_element(By.ID, 'account-submit').click()
def choose(self):
self.driver.switch_to.default_content()
self.driver.find_element(By.XPATH, '//*[@id="sform"]/div/div[3]/input[1]').clear()
self.driver.find_element(By.XPATH, '//*[@id="sform"]/div/div[3]/input[1]').send_keys(self.go)
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@id="sform"]/div/div[4]/input[1]').clear()
self.driver.find_element(By.XPATH, '//*[@id="sform"]/div/div[4]/input[1]').send_keys(self.to)
time.sleep(1)
js1 = 'document.getElementsByName("FDate")[1].removeAttribute("readonly")'
self.driver.execute_script(js1)
js2 = 'document.getElementsByName("RetFDate")[1].removeAttribute("readonly")'
self.driver.execute_script(js2)
self.driver.find_element(By.XPATH, '//*[@id="sform"]/div/div[5]/input').clear()
self.driver.find_element(By.XPATH, '//*[@id="sform"]/div/div[5]/input').send_keys(self.start)
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@id="sform"]/div/div[6]/input').clear()
self.driver.find_element(By.XPATH, '//*[@id="sform"]/div/div[6]/input').send_keys(self.end)
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@id="search-submit"]').click()
while (True):
time.sleep(10)
sr = self.driver.find_element(By.XPATH,
'/html/body/div[3]/div[3]/div/div[1]/div[3]/div[2]/div[1]/div[2]/div[3]/div[1]')
if (sr.text == ''):
print("无票,正在重新查询")
self.driver.find_element(By.XPATH, '/html/body/div[3]/div[2]/div/div/div[8]/a').click()
else:
break
def checkTicket(self):
time.sleep(10)
self.driver.find_element(By.XPATH,
'/html/body/div[3]/div[3]/div/div[1]/div[3]/div[2]/div[1]/div[2]/div[3]/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/input').click()
time.sleep(3)
self.driver.find_element(By.XPATH, '//*[@id="u-dialog-msg"]/div/div[2]/div[4]/span').click()
time.sleep(10)
self.driver.find_element(By.XPATH,
'/html/body/div[3]/div[3]/div/div[1]/div[3]/div[3]/div[1]/div[2]/div[3]/div[1]/div/div[2]/div[2]/div/div[2]/div/div[2]/input').click()
time.sleep(10)
self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/ul[1]/li/ul/li[1]/input').send_keys("谢冲")
time.sleep(3)
self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/ul[1]/li/ul/li[2]/input[2]').send_keys(
"17837752383")
time.sleep(3)
self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/ul[1]/li/ul/li[4]/input').send_keys(
"41132720000303491X")
time.sleep(1)
self.driver.find_element(By.XPATH, '//*[@id="J_agreement"]/input').click()
time.sleep(1)
self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/ul[3]/li[1]').click()
time.sleep(10)
self.driver.find_element(By.XPATH,
'/html/body/div[4]/div[3]/div[1]/div/div/table/tbody/tr[2]/td[1]/a').click()
time.sleep(1)
self.driver.find_element(By.XPATH,
'/html/body/div[4]/div[3]/div[1]/div/div/table/tbody/tr[2]/td[1]/a').click()
time.sleep(1)
self.driver.find_element(By.XPATH, '/html/body/div[4]/div[3]/div[2]/div[3]/div[2]/a').click()
time.sleep(5)
self.driver.find_element(By.XPATH, '/html/body/div[2]/div[5]/div[2]/div/div[1]/a[2]').click()
time.sleep(1)
self.driver.find_element(By.XPATH, '/html/body/div[11]/div[2]/div[2]/button[1]').click()
time.sleep(10)
self.huadong()
def huadong(self):
print('开始处理滑动验证码...')
track = [30, 50, 90, 140]
try:
slider = wait.WebDriverWait(self.driver, 5).until(
EC.presence_of_element_located((By.XPATH, '/html/body/div[13]/div[2]/div[6]/div/div[1]/div[2]/div[2]')))
ActionChains(self.driver).click_and_hold(slider).perform()
for i in track:
ActionChains(self.driver).move_by_offset(xoffset=i, yoffset=0).perform()
except:
print('验证码识别错误!等待验证码刷新,重新识别验证码...')
time.sleep(2.1)
time.sleep(100)
def main():
options = webdriver.ChromeOptions()
options.add_argument(
'user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 Edg/86.0.622.69"')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options)
driver.maximize_window()
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
"source": """
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
"""
})
url = 'https://www.ch.com/'
userName = 17837752383
password = 'password123'
go = '深圳'
to = '南阳'
start = '2022-04-11'
end = '2022-04-13'
buyTicket = BuyTicket(driver, url, userName, password, go, to, start, end)
buyTicket.login()
buyTicket.choose()
buyTicket.checkTicket()
if __name__ == '__main__':
main()
|