selenium模拟登录淘宝这招是行不通的吗?

clipboard.png

url = "https://sycm.taobao.com/custom/login.htm?_target=http://sycm.taobao.com/portal/home.htm"
options = webdriver.ChromeOptions()
# 此步骤很重要,设置为开发者模式,防止被各大网站识别出来使用了Selenium
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 不加载图片,加快访问速度
# options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
browser = webdriver.Chrome(executable_path="D:\chromedriver_win32\chromedriver.exe", options=options)
browser.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
  "source": """
    Object.defineProperty(navigator, 'webdriver', {
      get: () => undefined
    })
  """
})
browser.get(url)
time.sleep(2)

# 进入登录页面
# 当元素在iframe
browser.switch_to.frame(0)
username_ele = browser.find_element_by_css_selector("#fm-login-id")
password_ele = browser.find_element_by_css_selector("#fm-login-password")
submit_ele = browser.find_element_by_css_selector(".password-login")

username_ele.send_keys(username)
password_ele.send_keys(password)
submit_ele.click()

time.sleep(10)

用了网上的很多方法,selenium自动登录淘宝总是被检测到,是不是用selenium模拟登录淘宝这招是行不通的是吗?

阅读 389
评论
    1 个回答

    试了一下是可以正常登录的,没有要求滑块等任何验证。
    环境:火狐Developer 66.0b14 (64 位),电信ip

    部分注入脚本:

    function callInPageContent(func) {
        // run in html script
        // 此实现为FF特性, 仅在FF可用
        // 其他浏览器可以插入script来实现
        window.eval("(" + func.toSource() + ")();");
    }
    
    (() => {
        // run in content script
        callInPageContent(() => {
            Object.defineProperty(window.navigator, 'webdriver', {
                get: () => undefined
            });
            Object.defineProperty(window.navigator, 'platform', {
                get: () => 'Win64'
            });
            Object.defineProperty(window.navigator, 'hardwareConcurrency', {
                get: () => 4
            });
            Object.defineProperty(window.navigator, 'oscpu', {
                get: () => 'Win64'
            });
    
    
            const rawGetContext = window.HTMLCanvasElement.prototype.getContext;
            Object.defineProperty(window.HTMLCanvasElement.prototype, 'getContext', {
                get: () => function (s) {
                    if (s === 'experimental-webgl') {
                        return {
                            VENDOR: 7936,
                            RENDERER: 7937,
                            getExtension: function () {
                                return null;
                            },
                            getParameter: function (s) {
                                if (s === 7936 || s === 7937) {
                                    return 'Mozilla';
                                }
                                return null;
                            },
                            getSupportedExtensions: function () {
                                return [
                                    "ANGLE_instanced_arrays",
                                    "EXT_blend_minmax",
                                    "EXT_color_buffer_half_float",
                                    "EXT_texture_compression_rgtc",
                                    "EXT_frag_depth",
                                    "EXT_sRGB",
                                    "EXT_shader_texture_lod",
                                    "EXT_texture_filter_anisotropic",
                                    "OES_element_index_uint",
                                    "OES_standard_derivatives",
                                    "OES_texture_float",
                                    "OES_texture_float_linear",
                                    "OES_texture_half_float",
                                    "OES_texture_half_float_linear",
                                    "OES_vertex_array_object",
                                    // "WEBGL_color_buffer_float",
                                    // "WEBGL_compressed_texture_s3tc",
                                    // "WEBGL_compressed_texture_s3tc_srgb",
                                    // "WEBGL_debug_renderer_info",
                                    // "WEBGL_debug_shaders",
                                    // "WEBGL_depth_texture",
                                    // "WEBGL_draw_buffers",
                                    // "WEBGL_lose_context",
                                ];
                            }
                        };
                    }
                    return rawGetContext.call(this, s);
                }
            });
            const overwrite = function (name) {
                const OLD = window.HTMLCanvasElement.prototype[name];
                Object.defineProperty(window.HTMLCanvasElement.prototype, name, {
                    "value": function () {
                        const shift = {
                            'r': Math.floor(Math.random() * 10) - 5,
                            'g': Math.floor(Math.random() * 10) - 5,
                            'b': Math.floor(Math.random() * 10) - 5,
                            'a': Math.floor(Math.random() * 10) - 5
                        };
                        const width = this.width, height = this.height, context = this.getContext("2d");
                        const imageData = context.getImageData(0, 0, width, height);
                        for (let i = 0; i < height; i++) {
                            for (let j = 0; j < width; j++) {
                                const n = ((i * (width * 4)) + (j * 4));
                                imageData.data[n] = imageData.data[n] + shift.r;
                                imageData.data[n + 1] = imageData.data[n + 1] + shift.g;
                                imageData.data[n + 2] = imageData.data[n + 2] + shift.b;
                                imageData.data[n + 3] = imageData.data[n + 3] + shift.a;
                            }
                        }
                        context.putImageData(imageData, 0, 0);
                        return OLD.apply(this, arguments);
                    }
                });
            };
            overwrite('toBlob');
            overwrite('toDataURL');
    
    
            // noinspection JSUnresolvedVariable
            const originalQuery = window.navigator.permissions.query;
            // noinspection JSUnresolvedVariable
            window.navigator.permissions.query = (parameters) => (
                parameters.name === 'notifications' ?
                    Promise.resolve({state: Notification.permission}) :
                    originalQuery(parameters)
            );
    
    
            const getParameter = window.WebGLRenderingContext.prototype.getParameter;
            window.WebGLRenderingContext.prototype.getParameter = function (parameter) {
                // UNMASKED_VENDOR_WEBGL
                if (parameter === 37445) {
                    return 'Intel Open Source Technology Center';
                }
                // UNMASKED_RENDERER_WEBGL
                if (parameter === 37446) {
                    return 'Mesa DRI Intel(R) Ivybridge Mobile ';
                }
    
                return getParameter(parameter);
            };
        });
    
        // noinspection JSUnresolvedVariable
        window.document.addEventListener('mousemove', evt => window.wrappedJSObject._adb_plugin_mm_evt = evt);
    })();
    

    manifest.json:

    {
      "manifest_version": 2,
      "name": "fake context",
      "version": "1.0.0",
      "applications": {
        "gecko": {
          "id": "Fake-Context@example.com"
        }
      },
      "browser_action": {
        "default_title": "fake context"
      },
      "content_scripts": [
        {
          "matches": [
            "*://*/*"
          ],
          "js": [
            "content.js"
          ],
          "all_frames": true,
          "match_about_blank": true,
          "run_at": "document_start"
        }
      ]
    }
      撰写回答

      登录后参与交流、获取后续更新提醒