import time
import selenium
import random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
class taobao():
def __init__(self):
self.options = webdriver.ChromeOptions()
self.options.add_argument('disable-infobars')
self.options.add_experimental_option("excludeSwitches", ['enable-automation'])
self.options.add_argument('--disable-blink-features')
self.options.add_argument('--disable-blink-features=AutomationControlled')
self.options.add_argument('--disable-gpu')
#chromedriver路径
service = Service('C:/Users/Administrator.DESKTOP-B76MIPB/AppData/Local/Google/Chrome/Application/chromedriver.exe')
self.browser = webdriver.Chrome(service=service, options=self.options)
self.browser.maximize_window()
self.browser.implicitly_wait(5)
self.domain = 'http://www.taobao.com'
self.action_chains = ActionChains(self.browser)
def login(self, username, password):
self.browser.get(self.domain)
driver_wait = WebDriverWait(self.browser, 10)
# 点击'亲, 请登录', 进入登录界面
button = driver_wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'h')))
button.click()
# 输入用户名密码
username_sender = driver_wait.until(EC.presence_of_element_located((By.ID, 'fm-login-id')))
username_sender.send_keys(username)
password_sender = driver_wait.until(EC.presence_of_element_located((By.ID, 'fm-login-password')))
password_sender.send_keys(password)
time.sleep(5)
# 检查是否出现了滑动验证码,移动滑块
# 点击登录按钮
button = driver_wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'password-login')))
button.click()
time.sleep(2)
print('登录成功,呢称为:' + self.get_nickname())
def get_nickname(self):
self.browser.get(self.domain)
time.sleep(0.5)
try:
return self.browser.find_element(By.CLASS_NAME, 'site-nav-user').text
except:
return ''
if __name__ == '__main__':
tb = taobao()
tb.login('username', 'password') #用户名、密码