# encoding:utf-8
from PIL import Image
from selenium import webdriver
from selenium.webdriver import ActionChains
chrome_options = webdriver.ChromeOptions()
path = r'D:\project_91jiafangwan\chromedriver.exe'
browser = webdriver.Chrome(executable_path=path, chrome_options=chrome_options)
url = 'http://www.91jf.com/default.php?url=member&act=memberinfo'
browser.maximize_window() # 将浏览器最大化
browser.get(url)
slider = browser.find_element_by_xpath("//img[@onclick='show_user_login()']")
ActionChains(browser).click(slider).perform()
ActionChains(browser).release().perform()
# 截取当前网页并放到E盘下命名为printscreen,该网页有我们需要的验证码
browser.save_screenshot('D:\\printscreen.png')
imgelement = browser.find_element_by_xpath("//img[@src='default.php?act=code']") # 定位验证码
location = imgelement.location # 获取验证码x,y轴坐标
size = imgelement.size # 获取验证码的长宽
rangle = (int(location['x']), int(location['y']), int(location['x'] + size['width']),
int(location['y'] + size['height'])) # 写成我们需要截取的位置坐标
i = Image.open("D:\\printscreen.png") # 打开截图
frame4 = i.crop(rangle) # 使用Image的crop函数,从截图中再次截取我们需要的区域
frame4 = frame4.convert('RGB')
frame4.save('D:\\save.jpg') # 保存我们接下来的验证码图片 进行打码
#browser.close()
两年大概看此博客blog.codingnow.com/aee/