selenium登录淘宝

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/9/2 12:00
# @Author  : ypl
# @Site    : 
# @File    : login.py
# @Software: PyCharm


# -*- coding: utf-8 -*-
"""
Created on Sat Apr 11 09:34:41 2020

@author: Administrator
"""
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

from script.script import cookie_to_table


class AliLogin:

    def __init__(self, name, password, captcha=None):
        self.name = name
        self.password = password
        self.captcha = captcha
        self.cookies = ""
        self.chrome_options = Options()
        self.chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])

chrome_options.add_argument("start-maximized") # 初始化就最大化
chrome_options.add_argument('--ignore-certificate-errors')


        self.chrome_options.add_argument('--no-sandbox')
        self.chrome_options.add_argument('--disable-dev-shm-usage')
        # self.chrome_options.add_argument('--headless')
        # 设置selenium对象
        self.broswer = ""

    def run(self, store_id):
        """进入阿里登录页面"""
        url = 'http://sycm.taobao.com/'
        self.broswer = webdriver.Chrome(chrome_options=self.chrome_options)
        self.broswer.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
            "source": """
                    Object.defineProperty(navigator, 'webdriver', {
                      get: () => undefined
                    })
                  """
        })
        time.sleep(2)

        # 定位登陆元素位置
        name_input = 'fm-login-id'
        password_input = 'fm-login-password'
        self.broswer.get(url)
        time.sleep(1)
        iframe = self.broswer.find_elements_by_tag_name("iframe")[0]

        # 切换至iframe
        self.broswer.switch_to.frame(iframe)
        # 模拟浏览器动作
        bot1 = self.broswer.find_element_by_id(name_input)
        bot1.send_keys(self.name)
        time.sleep(1)
        bot2 = self.broswer.find_element_by_id(password_input)
        bot2.send_keys(self.password)
        time.sleep(2)
        try:
            # 定位滑块位置
            huakuai = self.broswer.find_element_by_id('nc_1_n1z')
            # 拖动滑块
            self.move_to_gap(huakuai)
        except Exception as e:
            pass
        finally:
            # 定位登录按钮以及点击
            bot3 = self.broswer.find_element_by_xpath("//button[@class='fm-button fm-submit password-login']")
            bot3.click()
            time.sleep(20)
            try:
                user_error = self.broswer.find_element_by_xpath('//*[@id="login-error"]/span[2]').text
                time.sleep(1)
                if len(user_error) < 2:
                    pass
                else:
                    self.broswer.quit()
                    return {'code': 403, "msg": '你输入的密码和账户名不匹配,请重新输入'}
            except:
                time.sleep(5)
                for cookie in self.broswer.get_cookies():
                    str_cookie = '{0}={1};'
                    str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value'))
                    self.cookies += str_cookie
                print(self.cookies)
                time.sleep(5)
                self.broswer.quit()
                cookie_to_table(self.cookies, store_id)
                return {'code': 200, "msg": self.cookies}

    def move_to_gap(self, slider):
        """滑块拖动slider是要移动的滑块,tracks是要传入的移动轨迹"""
        ActionChains(self.broswer).click_and_hold(slider).perform()
        # for x in tracks:
        ActionChains(self.broswer).move_by_offset(xoffset=208,yoffset=0).perform()
        time.sleep(0.5)
        ActionChains(self.broswer).release().perform()
原文地址:https://www.cnblogs.com/itBlogToYpl/p/13845093.html