VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • 爬虫(十二):图形验证码的识别、滑动验证码的识(4)

until(EC.presence_of_element_located((By.CSS_SELECTOR, '.geetest_slider_button')))
  • ActionChains(self.driver).click_and_hold(slider).perform()
  • for x in track: # 只有水平方向有运动 按轨迹移动
  • ActionChains(self.driver).move_by_offset(xoffset=x, yoffset=0).perform()
  • sleep(1)
  • ActionChains(self.driver).release().perform() # 松开鼠标
  • 1.3.7 完整代码

    
    	
    1. # -*- coding:utf-8 -*-
    2. from PIL import Image
    3. from time import sleep
    4. from selenium import webdriver
    5. from selenium.webdriver.common.by import By
    6. from selenium.webdriver import ActionChains
    7. from selenium.webdriver.support.wait import WebDriverWait
    8. from selenium.webdriver.support import expected_conditions as EC
    9. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    10.  
    11. headers = {
    12. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
    13. }
    14. chrome_options = webdriver.ChromeOptions()
    15. chrome_options.add_experimental_option('w3c', False)
    16. caps = DesiredCapabilities.CHROME
    17. caps['loggingPrefs'] = {'performance': 'ALL'}
    18.  
    19. class SliderVerificationCode(object):
    20. def __init__(self): # 初始化一些信息
    21. self.left = 60 # 定义一个左边的起点 缺口一般离图片左侧有一定的距离 有一个滑块
    22. self.url = 'https://passport.bilibili.com/login'
    23. self.driver = webdriver.Chrome(desired_capabilities=caps,options=chrome_options)
    24. self.wait = WebDriverWait(self.driver, 20) # 设置等待时间20秒
    25. self.phone = "17369251763"
    26. self.passwd = "abcdefg"
    27.  
    28. def input_name_password(self): # 输入账号密码
    29. self.driver.get(self.url)
    30. self.driver.maximize_window()
    31. input_name = self.driver.find_element_by_xpath("//input[@id='login-username']")
    32. input_pwd = self.driver.find_element_by_xpath("//input[@id='login-passwd']")
    33. input_name.send_keys("username")
    34. input_pwd.send_keys("passport")
    35. sleep(3)
    36.  
    37. def click_login_button(self): # 点击登录按钮,出现验证码图片
    38. login_btn = self.driver.find_element_by_class_name("btn-login")
    39. login_btn.click()
    40. sleep(3)
    41.  
    42. def get_geetest_image(self): # 获取验证码图片
    43. gapimg = self.wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_bg')))
    44. sleep(2)
    45. gapimg.screenshot(r'./captcha1.png')
    46. # 通过js代码修改标签样式 显示图片2
    47. js = 'var change = document.getElementsByClassName("geetest_canvas_fullbg");change[0].style = "display:block;"'
    48. self.driver.execute_script(js)
    49. sleep(2)
    50. fullimg = self.wait.until(
    51. EC.presence_of_element_located((By.CLASS_NAME, 'geetest_canvas_slice')))
    52. fullimg.screenshot(r'./captcha2.png')
    53.  
    54. def is_similar(self, image1, image2, x, y):
    55. '''判断两张图片 各个位置的像素是否相同
    
    相关教程