使用selenium实现模拟淘宝登陆

本文介绍了一种使用Selenium库实现的淘宝登录自动化方法。通过Python脚本控制Chrome浏览器,完成输入账号密码并点击登录的全过程。文章详细展示了如何设置浏览器选项以提高加载速度,并通过XPath和Class Name定位页面元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


class TaoBaoLogin(object):

def __init__(self, account, password):
self.options = webdriver.ChromeOptions()
# 设置开发者模式
self.options.add_experimental_option("excludeSwitches", ['enable-automation'])
# 不加载图片,快速访问
self.options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
self.driver = webdriver.Chrome(options=self.options)
self.url = "https://login.taobao.com/member/login.jhtml"
# 等待浏览器加载数据
self.wait = WebDriverWait(self.driver, 10, 0.5)
self.account = account
self.password = password

def __del__(self):
self.driver.quit()

def login(self):
self.driver.get(self.url)
try:
pwd_login_elem = self.wait.until(
EC.element_to_be_clickable((By.XPATH, "//div[@id='J_QRCodeLogin']/div[@class='login-links']/a[1]")))
pwd_login_elem.click()

wb_login_elem = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "weibo-login")))
wb_login_elem.click()

account_elem = self.wait.until(EC.presence_of_element_located((By.NAME,"username")))
account_elem.send_keys(self.account)

pwd_elem = self.wait.until(EC.presence_of_element_located((By.NAME,"password")))
pwd_elem.send_keys(self.password)

submit = self.wait.until(EC.element_to_be_clickable((By.XPATH,"//a[@class='W_btn_g']")))
submit.click()

print("登陆成功")

except:
print("登陆失败")


if __name__ == '__main__':
username = input("请输入你的微博账号>>")
password = input("请输入你的微博密码>>")
TaoBaoLogin(username, password).login()
--------------------- 

转载于:https://www.cnblogs.com/ly570/p/11007571.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值