2023 12306 Ticket Purchase Platform Automated Ticket Purchase 1 | Solving Login Problems

Table of contents

1. Description

1.1. Background

1.2. Description

2. Steps

2.1. Open the 12306 website

2.2. Enter account number and password

2.3. Click to log in now

2.4. Drag the slider for authentication

3. Results

3.1. Operation results

4. This section


1. Description

1.1. Background

It’s the annual Spring Festival travel time again, but I can’t always get tickets, which makes me irritated. This time, by solving the login problem of the 12306 ticket purchasing platform, we will complete the 12306 automated ticket purchasing process step by step. The following content is the first part.

1.2. Description

Operating system: win 10

Editor:pycharmedu

Language and version: python 3.10

Library used: selenium

Implementation idea: Use selenium to simulate human behavior and automate login

Recognize picture verification code: Super Eagle

Browser download and driver installation: Firefox download and driver installation

Browser crash explanation: It is most likely because the downloaded browser and browser driver versions are inconsistent. It is recommended to use a search engine to find the content shared by the boss to solve the problem.

The following URL will not be the real one, use it for testing, pay attention to identify it and change it yourself.

2. Steps

2.1. Open the 12306 website

driver = Firefox()
driver.get('请求的url')

2.2. Enter account number and password


driver.find_element(By.XPATH, '//*[@id="J-userName"]').send_keys('123456')
driver.find_element(By.XPATH, '//*[@id="J-password"]').send_keys('123456')

2.3. Click to log in now

driver.find_element(By.XPATH, '//*[@id="J-login"]').click()

2.4. Drag the slider for authentication

question:

12306 will perform anti-crawling and detect whether the browser webdriver started by selenium is True. If so, it will be displayed again no matter how you drag it.

Solution:

After checking a lot of information (mostly Chrome browsers are used on the Internet, and Firefox is rarely used, so it is difficult to find), I found a solution. After requesting the URL, perform a front-end rebound and set the webdriver to false. .

script = 'Object.defineProperty(navigator,"webdriver",{get:()=>false,});'
driver.execute_script(script)

push_btu = driver.find_element(By.XPATH, '/html/body/div[1]/div[4]/div[2]/div[2]/div/div/div[2]/div/div[1]/div[2]/span')
ActionChains(driver).drag_and_drop_by_offset(push_btu, 500, 0).perform()

After running the code and viewing it on the console, the result is:

3. Results

3.1. Operation results

The above picture appears, indicating that you have successfully logged in, and you can then prepare to check and purchase tickets.

4. This section

In 2023, when 12306 logged in, there was no need for a picture verification code. The prepared identification verification code was not used, but it was also much more convenient.

The knowledge required for this automated login is as follows:

1) Event chain;

2) The use of basic selenium controls, such as clicking, inputting, dragging sliders, etc.

        The above content is the first part of ticket purchase. Due to time constraints, there will be a series of content until automatic ticket purchase is fully realized. The complete source code will be released when automatic ticket purchase is fully realized. Criticisms and corrections are welcome.

Guess you like

Origin blog.csdn.net/qq_57663276/article/details/128493008