selenium登陆某宝的滑动怎么过

selenium登陆淘宝的滑动怎么过,确实淘宝在滑动这一块限制了很多条件

没太多废话,上效果,在分析过程,上传code

由于现在大型网站对selenium工具进行检测,若检测到selenium,则判定为机器人,访问被拒绝。所以第一步是要防止被检测出为机器人,如何防止被检测到呢?当使用selenium进行自动化操作时,在chrome浏览器中的consloe中输入windows.navigator.webdriver会发现结果为Ture,而正常使用浏览器的时候该值为False。所以我们将windows.navigator.webdriver进行屏蔽。
同时也要构造模拟器更多的参数,有些参数不是为了伪造,而是为了便于调试了运行。

# -*- encoding: utf-8 -*-
# auth : 漫天、飞雪
# func : simulation crack taobao slide verification code

import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

user = "******"
password = "******"


class ChromeOption(object):
    def __init__(self):
        options = Options()
        options.add_argument('--user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36"')
        # options.add_argument('User-Agent="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"')
        # options.add_argument("--disable-javascript")  # 禁止加载js
        # options.add_argument("--window-size=1366,768")  # 设置窗口大小
        options.add_argument("--start-maximized")       # 最大化
        options.add_argument("--incognito")  # 隐身模式,无痕模式
        options.add_argument("--disable-infobars")  # 禁用浏览器正在被自动化程序控制的提示
        options.add_argument('lang=zh_CN.UTF-8')
        # options.add_argument('--disable-gpu')  # 禁用 gpu
        options.add_argument('--disable-infobars')  # 除去“正受到自动测试软件的控制”
        # options.set_headless()  # 无头浏览器
        # options.add_argument('--headless')  # 无头浏览器
        options.add_argument('--no-sandbox')  # 让 driver 在 root 权限下跑
        # options.add_argument('--disable-dev-shm-usage')
        # options.add_argument('blink-settings=imagesEnabled=false')  # 禁止加载图片
        options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 打开开发者模式
        options.add_argument("--auto-open-devtools-for-tabs")  # 打开 F12

        self.driver = webdriver.Chrome(options=options)

    def __del__(self):
        """ 关闭模拟器 """
        self.driver.close()

    @staticmethod
    def get_track(distance):
        """ 模拟轨迹 假装是人在操作 """

        """ 1.设定长度比例 """
        pos = [0, 1, 2, 3, 3, 2, 1, 2, 2, 1]  # 滑动轨迹之间比例设定
        pos = [0, 5]  # 滑动轨迹之间比例设定

        """ 2. 正弦函数 """
        # pos = [random.randrange(0, 10) for i in range(10)]
        # pos.sort()
        # pos = [item / 10 * math.pi for item in pos]
        # pos = [math.sin(x) for x in pos]

        pos_sum = sum(pos)
        route = [int(int(distance) * (item / pos_sum)) for item in pos]  # 计算出移动路径

        route = route + [int(distance) - sum(route), ]

        print('distance', distance)
        print('sum route', sum(route))
        print('route', route)
        return route

    def run(self):
        first_page_url = "https://www.taobao.com/"
        self.driver.get(url=first_page_url)
        login_url = "https://login.taobao.com/member/login.jhtml?spm=a21bo.2017.754894437.1.5af911d9Dscpe3&f=top&redirectURL=https%3A%2F%2Fwww.taobao.com%2F"
        self.driver.get(url=login_url)
        driver_element_user =self.driver.find_element_by_xpath('//input[@id="fm-login-id"]')
        driver_element_password =self.driver.find_element_by_xpath('//input[@id="fm-login-password"]')
        driver_element_user.send_keys(user)
        driver_element_password.send_keys(password)
        time.sleep(0.5)
        login_button = self.driver.find_element_by_xpath('//*[@id="login-form"]/div[@class="fm-btn"]/button[@type="submit"]')
        login_button.click()  # 登录,出验证码
        time.sleep(1)
        driver_element_slider = self.driver.find_element_by_xpath('//*[@id="nc_1_n1z"]')
        ActionChains(driver=self.driver).click_and_hold(on_element=driver_element_slider).perform()

        track_list = self.get_track(258)
        for track in track_list:
            ActionChains(driver=self.driver).move_by_offset(xoffset=track, yoffset=0).perform()  # 鼠标移动到距离当前位置(x,y)
            # time.sleep(0.002)  # 适当的延时迷惑敌人
        time.sleep(1)
        ActionChains(driver=self.driver).release(on_element=driver_element_slider).release()
        login_button.click()  # 登录
        time.sleep(1)
        commodity_url = "https://detail.tmall.com/item.htm?id=44081513406&ali_trackid=30_64ee7d9987b80c1ba85ae7f824e5d46c&spm=a21bo.2017.201863-2.1&skuId=78818774201"
        self.driver.get(url=commodity_url)
        driver_element_purchase = self.driver.find_element_by_xpath('//*[@id="J_LinkBuy"]')
        driver_element_purchase.click()
        time.sleep(3)
        driver_element_dingdan = self.driver.find_element_by_xpath('//*[@id="submitOrderPC_1"]/div/a')
        driver_element_dingdan.click()
        time.sleep(10)
        pass


if __name__ == '__main__':
    chrome = ChromeOption()
    chrome.run()


ps : 代理容错能力很弱,不鲁棒,只为了追求哪个效果。

你可能感兴趣的:(Selenium,Python,Chrome,selenium,chrome,python)