模拟登录时出现的滑动验证码需要将navigator.webdriver设置成false,如果selenium模拟登录就会变成true,绝大部分网站检测的就是这个。

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
import base64
import requests

options = webdriver.ChromeOptions()
# 就是这一行告诉chrome去掉了webdriver痕迹,令navigator.webdriver=false
options.add_argument("--disable-blink-features=AutomationControlled")

driver = webdriver.Chrome(options=options)
driver.implicitly_wait(10)
driver.maximize_window()
driver.get('https://www.zhihu.com/signin?next=%2Ffollow')
p_login = driver.find_element(By.XPATH, '//*[@id="root"]/div/main/div/div/div/div/div[2]/div/div[1]/div/div[1]/form/div[1]/div[2]')
p_login.click()
username = driver.find_element(By.XPATH, '//*[@id="root"]/div/main/div/div/div/div/div[2]/div/div[1]/div/div[1]/form/div[2]/div/label/input')
password = driver.find_element(By.XPATH, '//*[@id="root"]/div/main/div/div/div/div/div[2]/div/div[1]/div/div[1]/form/div[3]/div/label/input')
login = driver.find_element(By.XPATH, '//*[@id="root"]/div/main/div/div/div/div/div[2]/div/div[1]/div/div[1]/form/button')
username.send_keys('xxxxxxxxx')
password.send_keys('xxxxxxxxx')
sleep(0.5)
login.click()
sleep(0.5)
code_bg_src = driver.find_element(By.CLASS_NAME, 'yidun_bg-img').get_attribute('src')
code = driver.find_element(By.CLASS_NAME, 'yidun_jigsaw')
code_src = driver.find_element(By.CLASS_NAME, 'yidun_jigsaw').get_attribute('src')

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded',
}

# 验证背景
bytes_bg = requests.get(url=code_bg_src, headers=headers)
# 滑块
bytes_code = requests.get(url=code_src, headers=headers)

data = {
    'slide_image': base64.b64encode(bytes_code.content),
    'background_image': base64.b64encode(bytes_bg.content),
    'token': '用自己云码的token',
    'type': 20111,
}

response = requests.get('http://api.jfbym.com/api/YmServer/customApi', headers=headers, data=data)
# 通过云码
x = int(response.json().get('data').get('data'))
print(x)

# 按住滑块
actions = ActionChains(driver).click_and_hold(code)
actions.move_by_offset(xoffset=x, yoffset=0).perform()
# 最后释放滑块
actions.release().perform()

sleep(2)
driver.close()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.