完全没思路了
来源:2-22 如何将整个注册流程脚本进行模块化实战讲解

慕慕0417486
2018-11-20
#coding = utf-8
from selenium import webdriver
import sys
sys.path.append(‘D:/immoc_selenium’)
import time
import random
from PIL import Image
from ShowapiRequest import ShowapiRequest
from findElement import FindElement
class RegisterFunction(object):
def init(self,url):
self.driver = self.get_driver(url)
#获取driver并且打开url
def get_driver(self,url):
driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
return driver
#输入用户信息
def send_user_info(self,key,data):
self.get_user_element(key).send_keys(data)
#定位用户信息,获取element
def get_user_element(self,key):
find_element = FindElement(self.driver)
user_element = find_element.get_element(key)
return user_element
# 获取随机数
def get_range_usr(self):
user_info = ''.join(random.sample('12345678asdfgh', 8))
return user_info
#获取图片
def get_code_image(self,file_name):
self.driver.save_screenshot(file_name)
code_element = self.get_user_element('user_code')
left = code_element.location['x']
top = code_element.location['y']
right = code_element.size['width'] + left
height = code_element.size['height'] + top
im = Image.open(file_name)
# 裁剪图片
img = im.crop((left, top, right, height))
img.save(file_name)
#解析图片
def code_online(self,file_name):
self.get_code_image(file_name)
r = ShowapiRequest("http://route.showapi.com/184-5", "62626", "d61950be50dc4dbd9969f741b8e730f5")
r.addBodyPara("img_base64", "")
r.addBodyPara("typeId", "35")
r.addBodyPara("convert_to_jpg", "0")
r.addBodyPara("needMorePrecise", "0")
r.addFilePara("image", file_name) # 文件上传时设置
res = r.post()
text = res.json()['showapi_res_body']['Result']
#text = '34ds'
return text
def main(self):
user_name = self.get_range_usr()
user_email = user_name + '@163.com'
file_name = 'D:/immoc/test.png'
code_text = self.code_online(file_name)
self.send_user_info('user_email',user_email)
self.send_user_info('user_name',user_name)
self.send_user_info('user_password','111111')
self.send_user_info('user_code_input', code_text)
self.get_user_element('register_button').click()
time.sleep(3)
self.driver.close()
if name == ‘main’:
register_function = RegisterFunction(‘http://www.5itest.cn/register?goto=/’)
register_function.main()
D:\immoc_selenium\venv\Scripts\python.exe D:/immoc_selenium/register_function.py
Traceback (most recent call last):
File “D:/immoc_selenium/register_function.py”, line 74, in
register_function.main()
File “D:/immoc_selenium/register_function.py”, line 64, in main
code_text = self.code_online(file_name)
File “D:/immoc_selenium/register_function.py”, line 49, in code_online
self.get_code_image(file_name)
File “D:/immoc_selenium/register_function.py”, line 38, in get_code_image
left = code_element.location[‘x’]
AttributeError: ‘NoneType’ object has no attribute ‘location’
Process finished with exit code 1
1回答
-
Mushishi
2018-11-20
AttributeError: ‘NoneType’ object has no attribute ‘location’ 这个说明你的对象是一个none,那你的元素就没有拿到。你code_element有问题
00
相似问题