Use selenium to automatically log in to Taobao

#encoding=utf-8

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains

from selenium.common.exceptions import UnexpectedAlertPresentException

import time,unittest, re

#log in page

driver=webdriver.Firefox()

driver.get("https://login.taobao.com")

#Enter account password mode

driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div/div/div[2]/div[4]/div/div[5]/a').click()

driver.find_element_by_xpath('/html/body/div/div[2]/div[3]/div/div/div[2]/div[3]/form/div[2]/span').click()

driver.find_element_by_id('TPL_username_1').send_keys('账号')

#xpath for account name input, By for password input, skin it

driver.find_element_by_id("TPL_password_1").click()

driver.find_element_by_id("TPL_password_1").send_keys('密码')

driver.find_element_by_id("J_SubmitStatic").click()# Login button

 

So far, it stands to reason that if it is done manually, it is already possible to log in, but when running automation, it is found that the slider appears

As shown above, so you need to enter the password again and slide the slider

 

# Need a slider, log in again, enter the password first, then slide the slider

driver.find_element_by_id("TPL_password_1").click()

driver.find_element_by_id("TPL_password_1").send_keys('密码')

time.sleep(1)

dragger=driver.find_element_by_id('nc_1_n1z')#. Slider positioning

action=ActionChains(driver)

 

for index in range(500):

    try:

        action.drag_and_drop_by_offset(dragger, 500, 0).perform()# Move the mouse in parallel, here directly set a value beyond the range, so that an error will be reported after pulling to the end and the action will end

    except UnexpectedAlertPresentException:

        break

    time.sleep(11) #Wait for pause time

 

driver.find_element_by_id('J_SubmitStatic').click()#Re-press login button

 

print "finish"

 

At the beginning of this script, the following situation often occurs, and then it does not move. Even if the slider is manually pulled, it will report an error and cannot log in.

After being reminded by my friends, it was because the browser version was too low, and I thought it was Tencent's robot to identify the culprit.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324855607&siteId=291194637