Python crawler: Selenium and action chain realize Taobao simulated login

Posted Jun 28, 20205 min read

  1. Preparation

Without further ado, let s first take a look at the Taobao landing page. You can see that the login URL is too long, but in fact, the ones after the "?" can be discarded and get https://login.taobao.com/member/login.jhtml

As far as login is concerned, the page defaults to password login, which saves us some trouble. At present, as long as you find the account number and password box assignment using Selenium, you can click the login button to realize Taobao simulated login, but this is not the case. , We then look down.
Insert picture description here
When I used the following code to test and log in, I was surprised to find more slider verification codes.
Insert picture description here
What is the verification code of the slider? If you roll up your sleeves, you are doing it! ! !

from selenium import webdriver
import time
from PIL import Image
from selenium.webdriver import ActionChains

#initial
def main():
    bro = webdriver.Chrome()
    bro.maximize_window()

    bro.get("https://login.taobao.com/member/login.jhtml")
    time.sleep(1)

    bro.find_element_by_name("fm-login-id").send_keys("Taobao account")
    time.sleep(1)
    bro.find_element_by_name("fm-login-password").send_keys("Taobao password")
    time.sleep(1)
    time.sleep(10)


if __name__ == "__main__":
    main()

Insert picture description here

It is actually not difficult to crack the slider verification code. Generally speaking, it is necessary to find the slider verification code and then use the action chain to click and slide, and then click login to OK.
Then start code analysis.

Second, code analysis

first part
Insert picture description here

#initial
def main():
    bro = webdriver.Chrome()
    bro.maximize_window()

    bro.get("https://login.taobao.com/member/login.jhtml") #get way to request Taobao landing page
    time.sleep(1) #Sleep for one second, don't go too fast

    bro.find_element_by_name("fm-login-id").send_keys("Taobao account") Find account input tag according to name
    time.sleep(1)
    bro.find_element_by_name("fm-login-password").send_keys("Taobao password") find the password input tag according to the name
    time.sleep(1)

    GetImage(bro) #Get the slider screenshot

the second part

#Obtain
def GetImage(bro):
    # save_screenshot is to take a screenshot of the current page and save it
    bro.save_screenshot('taobao.png')

    code_img_ele = bro.find_element_by_xpath("//*[@id='nc_1__scale_text']/span") #Find the slider verification code according to the xpath syntax
    location = code_img_ele.location #Verification code picture upper left corner coordinates x,y
    size = code_img_ele.size # length and width corresponding to the label of the verification code
    # Coordinates of upper left corner and lower right corner
    rangle =(
        int(location['x']),int(location['y']),int(location['x']+ size['width']),int(location['y']+ size['height '])
   )

    i = Image.open("./taobao.png")
    # code_img_name ='./tb.png'
    # crop
    frame = i.crop(rangle) #Get the slider verification code image
    # frame.save(code_img_name)

    Action(bro,code_img_ele) #Execute

Pay attention to the timing of screenshots. The slider verification code will not appear after the login page starts to load, and will not appear until the account and password are entered.

Insert picture description here

I originally wanted to find the slider verification code using clss values, but there are two of the same class, so I chose to find it according to the xpath syntax.

the third part

There is not much to do here. The next thing to do is to click the slider and move it to the right to verify.

#carried out
def Action(bro,code_img_ele):
    # Action chain
    action = ActionChains(bro)
    # Long press and click
    action.click_and_hold(code_img_ele)

    # move_by_offset(x,y) x horizontal direction, y vertical direction
    # perform()Let the action chain execute immediately
    action.move_by_offset(300, 0).perform() #The reason for filling in 300 can be seen in the figure below
    time.sleep(0.5)

    # Release action chain
    action.release()
    # log in
    bro.find_element_by_xpath("//*[@id='login-form']/div[4]/button").click() #Find the login button according to the xpath syntax and click login
    time.sleep(10)
    bro.quit() #Close the browser

Insert picture description here
Even if it is over here, the automatic login function can be realized, and there is still a small sense of accomplishment!
It happens that today is the Dragon Boat Festival, and here I wish you all a happy Dragon Boat Festival.
Insert picture description here

The complete code is as follows:

from selenium import webdriver
import time
from PIL import Image
from selenium.webdriver import ActionChains

#initial
def main():
    bro = webdriver.Chrome()
    bro.maximize_window()

    bro.get("https://login.taobao.com/member/login.jhtml")
    time.sleep(1)

    bro.find_element_by_name("fm-login-id").send_keys("13597971392")
    time.sleep(1)
    bro.find_element_by_name("fm-login-password").send_keys("wad07244058664")
    time.sleep(1)

    GetImage(bro)

#================================================= ==================================

#Obtain
def GetImage(bro):
    # save_screenshot is to take a screenshot of the current page and save it
    bro.save_screenshot('taobao.png')

    code_img_ele = bro.find_element_by_xpath("//*[@id='nc_1__scale_text']/span")
    location = code_img_ele.location #Verification code picture upper left corner coordinates x,y
    size = code_img_ele.size # length and width corresponding to the label of the verification code
    # Coordinates of upper left corner and lower right corner
    rangle =(
        int(location['x']),int(location['y']),int(location['x']+ size['width']),int(location['y']+ size['height '])
   )

    i = Image.open("./taobao.png")
    # code_img_name ='./tb.png'
    # crop
    frame = i.crop(rangle)
    # frame.save(code_img_name)

    Action(bro,code_img_ele)

#================================================= ==================================

#carried out
def Action(bro,code_img_ele):
    # Action chain
    action = ActionChains(bro)
    # Long press and click
    action.click_and_hold(code_img_ele)

    # move_by_offset(x,y) x horizontal direction, y vertical direction
    # perform()Let the action chain execute immediately
    action.move_by_offset(300, 0).perform()
    time.sleep(0.5)

    # Release action chain
    action.release()
    # log in
    bro.find_element_by_xpath("//*[@id='login-form']/div[4]/button").click()
    time.sleep(10)
    bro.quit() #Close the browser

if __name__ == "__main__":
    main()

I am also a novice. I can point out the mistakes. If you think it is okay, you can like it and support it. Thank you readers.