Use Python and selenium automation simulated landing 12306 official website!

In recent years, more and more anti-climb to 12306 of serious, after obtaining a parameter tk years ago to increase the JS, CSS and other encryption!

At present, most people use the landing approach is the use of selenium, this article is no exception.

surroundings:

       Windows

  python 3.6.5

Modules:

     selenium

  pyautogui

     time

 

first step:

  Instantiate a browser and enter the 12306 official website

driver = webdriver.Chrome()
driver.get('https://kyfw.12306.cn/otn/resources/login.html')
driver.implicitly_wait(10)
driver.maximize_window()

 

 

Step two:

      Click account login

driver.find_element_by_xpath('/html/body/div[2]/div[2]/ul/li[2]').click()

 

    

 

third step:

  In the input box, analog input account password:

driver.find_element_by_xpath('//*[@id="J-userName"]').send_keys('123456')
driver.find_element_by_xpath('//*[@id="J-password"]').send_keys('123456')

 

 

 

 

the fourth step:

  When the account password is entered, we should do is simulate a click on the picture verification code!

       So first download the picture:

yzm_code = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/div[3]/div')
yzm_code.screenshot('yzm.png')

 

 

the fifth step:

  Verification code downloaded after you can do whatever they want, whether it is a code docking platform Ye Hao, their own training model, or use the code to simulate a click is also possible!

       The first two methods can not simply say, the third embodiment used here, simulated clicks.

  Here we talk about the need to achieve results, a total of eight map on this code, then I want to achieve the effect that I entered the program automatically click 1,2,3 1,2,3 three pictures.

  So let's get to the coordinates of the center of each picture, there is a regular, intelligent people must understand at a glance.

  I was just here to write this:

INPUT = code ( ' Please enter this code: ' ) 
the time.sleep ( . 5 ) 
point_map = {
     ' . 1 ' : ' 40,45 ' ,
     ' 2 ' : ' 116,53 ' ,
     ' . 4 ' : ' 257,50 ' ,
     ' 5 ' : ' 40,121 ' ,
     ' 6 ' : ' 116,133 ' ,
     '3': '185,52',
    '7': '185,132',
    '8': '257,130'
}

 

def get_point(indexs):
    indexs = indexs.split(',')
    temp = []
    for index in indexs:
        temp.append(point_map[index])
        print(temp)
    return temp


temp = get_point(code)

This two strings of code that is not explained, very basic things.
The net effect is to enter 1, 2, then you will get is a list:

[ '40,45','116,53']

Remember, this is only the coordinates of the verification code on the picture coordinates, not the global coordinates of the entire screen! ! ! !

So how do you get the global coordinates? Codes vertex (1206,428) + verification code coordinates, i.e., global coordinates! Here's (1206,428) Please test yourself!

 

 

 

 

 Step Six:

  Simulate a click, use this module to pyautogui. Remember, before using this module must be added the delay switch the screen.

  (This article only way to learn, and no other meaning, are normally a code docking platform! Hope that we can apply their knowledge in other programming process used to these methods!)

for i in temp:
    indexs = i.split(',')
    x = int(indexs[0])
    y = int(indexs[1])
    pyautogui.click(1206+x, 428+y)


       Finally, click Sign in!

driver.find_element_by_xpath('//*[@id="J-login"]').click()

 

Guess you like

Origin www.cnblogs.com/qdsn/p/11622197.html