Introduction

Target website: https://www.thaiticketmajor.com/concert/
Tools: Python + Selenium
Output: Simulate the process of purchasing tickets as real user, realizing automatic login, seat selection according to priority, filling in real-name information, and exporting information screenshots.

User_info:

test = {
    'name': ['xxx', 'qqq', 'sss'],
    'usr': 'test@163.com',
    'pwd': 'test123',
    'amount': 1,
    'phone': '12345678',
    'file': 'test',
    'prefer': ['#fixed.php#C3', '#fixed.php#E3G', '#fixed.php#E3K', '#fixed.php#E3J']
}



Main Program:

from selenium import webdriver
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
#import the account info of user
from usr_info.JIALYU_HOU import info
import pickle
import os

#create driver
class main():
    def __init__(self, home_url, target_url, info_usr):
        options = webdriver.ChromeOptions()
        options.add_argument("--disable-blink-features=AutomationControlled")
        # No picture mode, to speed up
        # options.add_argument("--blink-settings=imagesEnabled=false")
        options.add_argument("--start-maximized")
        self.home_url = home_url
        self.target_url = target_url
        self.info_usr = info_usr
        self.driver = webdriver.Chrome(options=options)
        self.wait = WebDriverWait(self.driver, 30)
        self.get_cookies()
        
    def get_cookies(self):
        self.driver.get(self.home_url)
        #located the login button
        login_button = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ' .mh-col.col-12.col-xl .btn-signin.item.d-none')))
        login_button.click()
        
        login_list = self.wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ' .box-input-group .input-txt')))
        #input the account's info
        login_list[0].send_keys(self.info_usr.info['usr'])
        login_list[1].send_keys(self.info_usr.info['pwd'])
        #click to ligin
        self.driver.find_element(By.CSS_SELECTOR, ' .txtC .btn-red.btn-signin').click()
        #set sleep time
        time.sleep(1)
        print('登录成功')
        self.driver.refresh()
        time.sleep(1.5)
        self.driver.get(self.target_url)
        if len(self.driver.find_elements(By.CSS_SELECTOR, ' .box-input-group .input-txt'))==1:
            login_list[0].send_keys(self.info_usr.info['usr'])
            login_list[1].send_keys(self.info_usr.info['pwd'])
            self.driver.find_element(By.CSS_SELECTOR, ' .txtC .btn-red.btn-signin').click()
        else:
        # while self.driver.title.find('Official Ticket | Jay Chou Carnival World Tour 2023 – Bangkok') != -1:
        #     time.sleep(1)
            self.parse()
            forzen = input('<<<')
            
    def parse(self):
        flash_flag = True
        while flash_flag:
            try:
                #select the show you would like to take part in
                time.sleep(0.5)
                show_test = self.wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ' .box-event-list .btn-item')))
                self.driver.execute_script("arguments[0].scrollIntoView();", show_test[0])
                show_test[0].click()
                time.sleep(0.5)
                
                #accept the agreement
                if len(self.driver.find_elements(By.CSS_SELECTOR, ' .label-txt')) == 1:
                    accept = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ' .label-txt')))
                    self.driver.execute_script("arguments[0].scrollIntoView();", accept)
                    accept.click()
                    buy_button = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ' .row.justify-content-center .btn-solid-round5-blue.w-auto')))
                    buy_button.click()
                    time.sleep(0.5)
                
                #select the seat area
                flag = 0  # record the number of clike
                for c in self.info_usr.info['prefer']:
                    area = self.wait.until(EC.presence_of_element_located((By.XPATH, '//map//area[contains(@href, "{}")]'.format(c))))
                    area.click()
                    time.sleep(1)
                    cfm_button = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ' .row .col-auto .btn-red')))
                    if cfm_button.text == ' เลือกโซนอื่น':   #no selected seat in this area
                        cfm_button.click()   #turn back to select again
                        time.sleep(1)
                        break
                    else:
                        flag_while = 0
                        test_flag = flag_while
                        sit_list = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, '//tr[contains(@class, "P")]//td[@title]')))
                        #Buy one ticket
                        if self.info_usr.info['amount'] == 1:
                            for sit in sit_list:
                                self.driver.execute_script("arguments[0].scrollIntoView();", sit)
                                if len(sit.find_elements(By.XPATH, './div[@class="seatuncheck"]')) == 1:
                                    sit.click()
                                    flag += 1
                                    break
                        #Buy multiple tickets
                        else:
                            for sit in sit_list:
                                flag_while += 1
                                self.driver.execute_script("arguments[0].scrollIntoView();", sit)
                                if len(sit.find_elements(By.XPATH, './div[@class="seatuncheck"]')) == 1:
                                    if flag_while - test_flag == 1 or test_flag == 0:
                                        sit.click()
                                        test_flag = flag_while
                                        flag += 1
                                        #set the number of ticket you would like to buy
                                        if flag == self.info_usr.info['amount']: 
                                            break
                                    else:
                                        test_flag = 0
                                        flag = 0
                                        self.driver.refresh()
                                        time.sleep(1)
                        if flag == 0:    #no suitable seat in this area
                            back_button = self.driver.find_elements(By.CSS_SELECTOR, ' .row .col-auto .btn-action')
                            if len(back_button) == 0:
                                cfm_button.click()
                            else:
                                back_button[0].click()
                            time.sleep(1)
                        else:
                            sit_pic = self.driver.get_screenshot_as_png()
                            self.driver.find_elements(By.CSS_SELECTOR, ' .row .col-auto .btn-red')[0].click()
                            time.sleep(1)
                            
                            #real_name info input
                            form_list = self.wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ' .form .my-3 .box-input .input-txt')))
                            for n, i in enumerate(form_list):
                                i.send_keys(self.info_usr.info['name'][n])
                                
                            name_pic = self.driver.get_screenshot_as_png()
                            self.driver.find_element(By.CSS_SELECTOR, ' .form .btn-red').click()
                            flash_flag = False
                            time.sleep(1)
                            self.pay(sit_pic, name_pic)
                if flag == 0:
                    print('没票啦...')
                    
            except Exception as e:
                print(e)
                self.driver.refresh()
    
    def pay(self, sit, name):
        flash_flag2 = True
        while flash_flag2:
            try:
                #delivery
                pay_list = self.wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, ' .section.cls_section .col-6')))
                self.driver.execute_script("arguments[0].scrollIntoView();", pay_list[2])
                pay_list[2].click()
                time.sleep(0.5)
                #select payment method:
                self.driver.execute_script("arguments[0].scrollIntoView();", pay_list[4]) 
                self.driver.find_element(By.CSS_SELECTOR, ' .section.cls_section .col-6 #btn_alipay').click()
                #accept the agreement
                check_list = self.driver.find_elements(By.CSS_SELECTOR, ' .content-body .label-txt')
                self.driver.execute_script("arguments[0].scrollIntoView();", check_list[1])
                check_list[0].click()  #取消ticket protect
                self.driver.find_element(By.CSS_SELECTOR, ' .section.cls_section .col-6 #btn_alipay').click()
                check_list[1].click()
                self.driver.find_element(By.CSS_SELECTOR, ' .row .col-auto #btn_confirm').click()
                time.sleep(1)
                
                # select Ali Pay
                alipay = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ' .tab-pane #alipay')))
                alipay.click()
                # payment info:
                pay_name = self.driver.find_element(By.CSS_SELECTOR, '#walletDetails #wallet_payer_name').send_keys(
                    info.info['name'][0])
                pay_email = self.driver.find_element(By.CSS_SELECTOR, '#walletDetails #wallet_email_address').send_keys(
                    info.info['usr'])
                pay_phone = self.driver.find_element(By.CSS_SELECTOR, '#walletDetails #wallet_mobile_number').send_keys(
                    info.info['phone'])
                self.driver.find_element(By.CSS_SELECTOR, ' .form-actions #btnwalletsubmit').click()
                flash_flag2 = False
            except Exception as e:
                print(e)
                self.driver.refresh()
        #Booking successful
        flag_end = 0
        time.sleep(5)   #wait for the payment screenshot
        while self.driver.title.find('支付宝 - 网上支付 安全快速!') != -1:
            time.sleep(3)
            flag_end += 1
            if flag_end == 3:
                break
        payment_pic = self.driver.get_screenshot_as_png()
        self.save_pic(sit, name, payment_pic)
    
    def save_pic(self, sit, name, pay):
        with open('./usr_info/{}/sit.png'.format(self.info_usr.info['file']), 'wb') as f1, open('./usr_info/{}/name.png'.format(self.info_usr.info['file']), 'wb') as f2, open('./usr_info/{}/payment.png'.format(self.info_usr.info['file']), 'wb') as f3:
            f1.write(sit)
            f2.write(name)
            f3.write(pay)

if __name__ == '__main__':
    # home_url = 'https://www.thaiticketmajor.com/concert/'
    # test2 = 'https://www.thaiticketmajor.com/concert/2023-ok-taecyeon-in-bangkok-specialty.html'
    # url = 'https://www.thaiticketmajor.com/concert/jay-chou-carnival-world-tour-2023-bangkok.html'
    # test = 'https://www.thaiticketmajor.com/concert/Coldplay-music-of-the-spheres-in-bangkok.html'
    # run = main(home_url, test, info)



Multiprocess

from multiprocessing import Process
from main import main
from usr_info.JIALYU_HOU import info

home_url = 'https://www.thaiticketmajor.com/concert/'

def run_process1():
    # target_url = "https://www.thaiticketmajor.com/concert/2023-ok-taecyeon-in-bangkok-specialty.html"
    target_url = "https://www.thaiticketmajor.com/concert/jay-chou-carnival-world-tour-2023-bangkok.html"
    instance = main(home_url, target_url, info)

def run_process2():
    target_url = "https://www.thaiticketmajor.com/concert/Coldplay-music-of-the-spheres-in-bangkok.html"
    instance = main(home_url, target_url, info)

if __name__ == "__main__":
    process1 = Process(target=run_process1)
    # process2 = Process(target=run_process2)

    process1.start()
    # process2.start()

    # process1.join()
    # process2.join()

    print("Go Go Go")



Output: