How to Fully Automate Taobao Login with Selenium in Python

This article demonstrates a step‑by‑step Python Selenium solution for completely automating Taobao login, covering dependency setup, class design, handling the sliding captcha, and alternative login via Sina Weibo, complete with full source code and usage instructions.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Fully Automate Taobao Login with Selenium in Python

Preface

Previously I wrote a blog about crawling Taobao product information. At that time I was a beginner and manually logged into Taobao, letting the browser store my credentials, then used local user configuration to control the browser, which solved the login problem in a hacky way.

That approach turned Selenium from fully automatic to semi‑automatic, which does not showcase Python's elegance. The question is how to achieve fully automatic Taobao login.

Analysis

To make usage convenient, I encapsulated the entire code into a file named login.py with a class Login.

1) Dependencies

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium import webdriver
import time

2) Constructor

def __init__(self, username, password):
    """Initialize browser configuration and login credentials"""
    self.url = 'https://login.taobao.com/member/login.jhtml'
    # Initialize Chrome options
    options = webdriver.ChromeOptions()
    # Disable image loading
    options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
    # Enable developer mode
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    self.browser = webdriver.Chrome(options=options)
    # Set explicit wait time to 40 seconds
    self.wait = WebDriverWait(self.browser, 40)
    self.username = username  # username
    self.password = password  # password

3) Original login using Taobao account or phone number

def original(self):
    """Direct login with Taobao account"""
    self.browser.get(url=self.url)
    try:
        input_username = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.fm-field > div.input-plain-wrap.input-wrap-loginid > input')))
        input_password = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.fm-field > div.input-plain-wrap.input-wrap-password > input')))
        # Wait for the slider button to load
        div = self.wait.until(EC.presence_of_element_located((By.ID, 'nc_1__bg')))
        input_username.send_keys(self.username)
        input_password.send_keys(self.password)
        # Sleep 2 seconds for the slider to load
        time.sleep(2)
        # Click and hold the slider
        ActionChains(self.browser).click_and_hold(div).perform()
        # Move the slider horizontally 300 pixels
        ActionChains(self.browser).move_by_offset(xoffset=300, yoffset=0).perform()
        # Wait until verification passes
        self.wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, 'div#nc_1__scale_text > span.nc-lang-cnt > b'), '验证通过'))
        # Submit login
        input_password.send_keys(Keys.ENTER)
        print('Successful !')
    except TimeoutException as e:
        print('Error:', e.args)
        self.original()

The key element for the slider is the div with id nc_1__bg. Sometimes locating the span does not work, but using its parent div allows the drag action to succeed.

If the drag trajectory is not perfectly horizontal, the verification may fail. Therefore the script moves the slider only in the horizontal direction (300px, 0px) and adds a wait for the verification text.

4) Login with Sina Weibo account (clever use of a loophole)

def sina(self):
    """Login using a pre‑bound Sina Weibo account"""
    self.browser.get(url=self.url)
    try:
        # Wait for the Sina login link to load
        weibo_login = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#login-form a.weibo-login')))
        weibo_login.click()
        input_username = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.info_list > div.inp.username > input.W_input')))
        input_password = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.info_list > div.inp.password > input.W_input')))
        input_username.send_keys(self.username)
        input_password.send_keys(self.password)
        input_password.send_keys(Keys.ENTER)
        # Wait for the browser to save the information (increase if network is slow)
        time.sleep(5)
        # Refresh the page
        self.browser.refresh()
        # Wait for the quick login button to load
        quick_login = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.info_list > div.btn_tip > a.W_btn_g')))
        quick_login.click()
        print('login successful !')
    except TimeoutException as e:
        print('Error:', e.args)
        self.sina()

The login button on the page is a dummy link javascript:void(0), which cannot be clicked directly. Refreshing the page after entering credentials often triggers the saved session, allowing a quick login.

Full Code and Usage

1) Complete code

# -*- coding: utf-8 -*-
"""
@author: Pineapple
@contact: [email protected]
@time: 2020/7/28 9:09
@file: login.py
@desc: login taobao.
"""
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium import webdriver
import time

class Login:
    def __init__(self, username, password):
        """Initialize browser configuration and login information"""
        self.url = 'https://login.taobao.com/member/login.jhtml'
        options = webdriver.ChromeOptions()
        options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
        options.add_experimental_option('excludeSwitches', ['enable-automation'])
        self.browser = webdriver.Chrome(options=options)
        self.wait = WebDriverWait(self.browser, 40)
        self.username = username
        self.password = password

    def original(self):
        """Direct login with Taobao account"""
        self.browser.get(url=self.url)
        try:
            input_username = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.fm-field > div.input-plain-wrap.input-wrap-loginid > input')))
            input_password = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.fm-field > div.input-plain-wrap.input-wrap-password > input')))
            div = self.wait.until(EC.presence_of_element_located((By.ID, 'nc_1__bg')))
            input_username.send_keys(self.username)
            input_password.send_keys(self.password)
            time.sleep(2)
            ActionChains(self.browser).click_and_hold(div).perform()
            ActionChains(self.browser).move_by_offset(xoffset=300, yoffset=0).perform()
            self.wait.until(EC.text_to_be_present_in_element((By.CSS_SELECTOR, 'div#nc_1__scale_text > span.nc-lang-cnt > b'), '验证通过'))
            input_password.send_keys(Keys.ENTER)
            print('Successful !')
        except TimeoutException as e:
            print('Error:', e.args)
            self.original()

    def sina(self):
        """Login using Sina Weibo account (pre‑bound)"""
        self.browser.get(url=self.url)
        try:
            weibo_login = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#login-form a.weibo-login')))
            weibo_login.click()
            input_username = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.info_list > div.inp.username > input.W_input')))
            input_password = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.info_list > div.inp.password > input.W_input')))
            input_username.send_keys(self.username)
            input_password.send_keys(self.password)
            input_password.send_keys(Keys.ENTER)
            time.sleep(5)
            self.browser.refresh()
            quick_login = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div.info_list > div.btn_tip > a.W_btn_g')))
            quick_login.click()
            print('login successful !')
        except TimeoutException as e:
            print('Error:', e.args)
            self.sina()

2) Usage

from login import Login

username = '******'  # account
password = '******'  # password

login = Login(username, password)
# Use Taobao account or phone number login
login.original()
# Use Sina Weibo login (uncomment to use)
# login.sina()

Conclusion

This article shows a practical example of automating Taobao login, but it still relies on some hacky methods such as handling the javascript:void(0) dummy link and the sliding captcha. If Taobao strengthens its anti‑scraping mechanisms with newer captchas, the current solution may become obsolete, so further work on solving advanced captchas is needed.

If there are errors, feel free to send a private message for correction. Technology never ends, thank you for the support!

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonTaobaologinSeleniumWeb AutomationScraping
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.