发布时间:2020-01-20 18:14:59编辑:Run阅读(11638)
目标网站:aliexpress(全球速卖通)阿里国际站
登录url : https://login.aliexpress.com/
python selenium 输入用户名,密码 拖动滑块验证,因为这个不涉及到缺口,理论上是很简单的
配置好selenium的环境:
pip install selenium
下个对应google浏览器版本的chromedriver驱动,放在python安装目录下(win环境)
第一次尝试的代码:
from selenium import webdriver import time from selenium.webdriver import ActionChains def selenium_login_alibaba(self, url): option = webdriver.ChromeOptions() option.add_argument('--start-maximized') browser = webdriver.Chrome(chrome_options=option) browser.delete_all_cookies() browser.get(url) try: # 隐式等待 time.sleep(2) browser.implicitly_wait(20) time.sleep(0.1) browser.find_element_by_xpath("//input[@id='fm-login-id']").send_keys('username') time.sleep(0.2) browser.find_element_by_xpath("//input[@id='fm-login-password']").send_keys('password') time.sleep(0.3) # 获取滑块长,宽 button = browser.find_element_by_xpath("//span[@id='nc_1_n1z']") small_sliding = button.size # 获取整个div的长,宽 big_sliding = browser.find_element_by_xpath("//span[@class='nc-lang-cnt']").size # 滑动的距离 sliding_distance = big_sliding.get('width') - small_sliding.get('width') print(sliding_distance) ActionChains(browser).click_and_hold(button).perform() for i in [99, 87, 51, 59]: ActionChains(browser).move_by_offset(xoffset=i, yoffset=0).perform() time.sleep(random.random()) ActionChains(browser).release().perform() time.sleep(1) browser.find_element_by_xpath("//button[@class='fm-button fm-submit password-login']").click()
效果如下:
验证失败了,其实就是chromedriver被检测出来了.怎么解决呢?这里需要用一个打开exe程序的工具,对chromedriver里面的key进行更改,至于linux,mac应该也有类似工具。
(win环境工具) 下载地址: https://pan.baidu.com/s/1SnmXcCSeE2YT3Z8-Hey7yA 提取码 : wat7
找到解压目录,打开exe.
下一步用wxmedit.exe 打开 chromedriver.exe
查找$cdc
把cdc替换成abc(随意3个什么都行,不能多写也不能少写,长度一定要对应)
替换成功后保存,保存,保存....
接下来,重新来过,更改key后的尝试:
代码如下:
from selenium import webdriver import time from selenium.webdriver import ActionChains def selenium_login_alibaba(self, url): option = webdriver.ChromeOptions() option.add_argument('--disable-infobars') option.add_argument('--start-maximized') option.add_argument('user-agent="Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"') option.add_argument('--disable-extensions') option.add_argument('--profile-directory=Default') option.add_argument("--incognito") prefs = {"profile.managed_default_content_settings.images": 2} option.add_experimental_option("prefs", prefs) option.add_experimental_option('excludeSwitches', ['enable-automation']) browser = webdriver.Chrome(chrome_options=option) browser.delete_all_cookies() browser.get(url) try: # 隐式等待 time.sleep(2) browser.implicitly_wait(20) time.sleep(0.1) browser.execute_script('Object.defineProperties(navigator,{webdriver:{get:()=>false}})') browser.find_element_by_xpath("//input[@id='fm-login-id']").send_keys('username') time.sleep(0.2) browser.find_element_by_xpath("//input[@id='fm-login-password']").send_keys('password') time.sleep(0.3) # 获取滑块长,宽 button = browser.find_element_by_xpath("//span[@id='nc_1_n1z']") small_sliding = button.size # 获取整个div的长,宽 big_sliding = browser.find_element_by_xpath("//span[@class='nc-lang-cnt']").size # 滑动的距离 sliding_distance = big_sliding.get('width') - small_sliding.get('width') print(sliding_distance) ActionChains(browser).click_and_hold(button).perform() for i in [99, 87, 51, 59]: ActionChains(browser).move_by_offset(xoffset=i, yoffset=0).perform() time.sleep(random.random()) ActionChains(browser).release().perform() time.sleep(1) browser.find_element_by_xpath("//button[@class='fm-button fm-submit password-login']").click()
效果如下: 滑动验证成功了,这种可以解决大部分的chromedriver检测机制
欢迎加qq群交流: 198447500
47745
46235
37110
34627
29229
25886
24745
19863
19417
17908
5716°
6315°
5835°
5888°
6984°
5829°
5846°
6361°
6316°
7673°