Problems and solutions encountered by selenium simulating login to Taobao

今天忽然想用爬虫登陆淘宝,于是就试着写一下,

I used selenium to simulate login. I first opened the Taobao login page and found that Taobao’s login interface looked like this. Insert image description here
I wrote a simulated login using code: This is the final code:

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains


path = r'E:\新建文件夹\第四阶段\day06(1)\ziliao\chromedriver(1).exe'
##创建浏览器对象
options = webdriver.ChromeOptions()
#设置开发者模式
options.add_experimental_option('excludeSwitches', ['enable-automation'])
#不加载图片,加快访问速度
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
driver = webdriver.Chrome(executable_path=path, options=options)
driver.get("https://www.taobao.com/?spm=a2107.1.1000340.1.4a5c11d9BosTHg")
#隐式等待
driver.implicitly_wait(10)
#将浏览器放大
driver.maximize_window()
#点击登录账号密码
deng = driver.find_element_by_xpath("//div[@class='site-nav-sign']/a[@class='h']").click()
#点击到帐密登录
driver.find_element_by_id('J_Quick2Static').click()
#账号
myusername = driver.find_element_by_id('TPL_username_1')
myusername.send_keys('你的账号')
time.sleep(3)

#密码
mypassword = driver.find_element_by_id('TPL_password_1')
mypassword.send_keys("你的密码")
time.sleep(1)
#定位滑块
dragger = driver.find_element_by_id('nc_1_n1z')  # 滑块定位
action = ActionChains(driver)
for index in range(500):
    try:
        action.drag_and_drop_by_offset(dragger, 500, 0).perform()  # 平行移动鼠标,此处直接设一个超出范围的值,这样拉到头后会报错从而结束这个动作

    except Exception:
        break
    time.sleep(3)  # 等待停顿时间
#点击登录
print('*')
button = driver.find_element_by_id('J_SubmitStatic').click()
print('@')

The login is successful.
Insert image description here
I entered the account and password manually. If anyone can log in automatically or knows how to detect selenium anti-crawling, please leave a message in the comment area and let me know.

Guess you like

Origin blog.csdn.net/qq_39306128/article/details/90741031
Recommended