#_save_url 保存路径 def image_cj(driver, _save_url): try: _file_name = random.randint(0, 100000) _file_url_drop = _save_url + str(_file_name) + '背景.png' # 滑块背景存储地址 _file_url = _save_url + str(_file_name) + '验证.png' # 滑块存储地址 _pic_url_drop = driver.find_element_by_xpath('//*[@id="slideBkg"]') # 定位滑块背景图元素 mutex.acquire() get_pic(_pic_url_drop.get_attribute('src'), _file_url_drop) # 根据滑块背景图的url下载图片 time.sleep(1) mutex.release() _pic_url = driver.find_element_by_xpath('//*[@id="slideBlock"]') # 定位滑块元素 mutex.acquire() get_pic(_pic_url.get_attribute('src'), _file_url) # 根据滑块的url下载图片 time.sleep(1) mutex.release() file_path = [] # 定义滑块返回图片存储地址 file_path.append(_file_url) file_path.append(_file_url_drop) return file_path except Exception as e: print('错误 :', e) return str(_file_url) #下载图片 def get_pic(url, path): response = requests.get(url) # 获取的文本实际上是图片的二进制文本 img = response.content # 将他拷贝到本地文件 w 写 b 二进制 wb代表写入二进制文本 with open(path, 'wb') as f: f.write(img)