那个补充之后,运行又提示 find_element() argument after * must be an iterable, not WebElement
现在就是不知道这个 tuple 格式数据哪里来的,找不到这个数据
就是 str 也会报这个错误
<class 'str'>
//span[@class = "ant-input-affix-wrapper ant-input-affix-wrapper-lg"]/input[@placeholder='请输入帐户名']
<class 'str'>
//span[@class = "ant-input-affix-wrapper ant-input-affix-wrapper-lg"]/input[@placeholder='请输入密码']
<class 'str'>
//span[@class = "ant-input-affix-wrapper ant-input-affix-wrapper-lg"]/input[@placeholder='请输入验证码']
<class 'str'>
//button[@class='login_btn']
Traceback (most recent call last):
File "D:/dingdang_project/DD_changtu/libs/login_business.py", line 45, in <module>
login_busin(user='ctz', passwd='admin123456?')
File "D:/dingdang_project/DD_changtu/libs/login_business.py", line 31, in login_busin
br.__find_element__(lc='xpath', locator=username).send_keys(user)
AttributeError: 'tuple' object has no attribute 'send_keys'
Process finished with exit code 1
username = xpath->//span[@class = "ant-input-affix-wrapper ant-input-affix-wrapper-lg"]/input[@placeholder='请输入帐户名']
password = xpath->//span[@class = "ant-input-affix-wrapper ant-input-affix-wrapper-lg"]/input[@placeholder='请输入密码']
Verification_Code = xpath->//*[@class = "user-layout-login ant-form ant-form-horizontal"]/div[3]/div[2]/img
Verification = xpath->//span[@class = "ant-input-affix-wrapper ant-input-affix-wrapper-lg"]/input[@placeholder='请输入验证码']
Login = xpath->//button[@class='login_btn']
def get_locators_or_account(self, section, option):
"""获取指定section, 指定option对应的数据, 返回元祖和字符串"""
try:
locator = self.conf.get(section, option)
if ('->' in locator):
locator = tuple(locator.split('->'))
return locator[1]
except configparser.NoOptionError as e:
print('error:', e)
return 'error: No option "{}" in section: "{}"'.format(option, section)
使用这个方法获取这个 xpath 地址信息,查了一下 type,也是 str 类型
这个把定位的方法写在一块了
def __find_element__(self, lc, locator):
"""元素定位"""
if lc == "id":
ele = self.driver.find_element(By.ID, locator)
wdw(self.driver, 10).until(ec.visibility_of_element_located(ele))
elif lc == "name":
ele = self.driver.find_element(By.NAME, locator)
wdw(self.driver, 10).until(ec.visibility_of_element_located(ele))
elif lc == "xpath":
ele = (By.XPATH, locator)
wdw(self.driver, 10).until(ec.visibility_of_element_located(ele))
elif lc == "tag_name":
ele = self.driver.find_element(By.TAG_NAME, locator)
wdw(self.driver, 10).until(ec.visibility_of_element_located(ele))
elif lc == "link_text":
ele = self.driver.find_element(By.LINK_TEXT, locator)
wdw(self.driver, 10).until(ec.visibility_of_element_located(ele))
elif lc == "partial_link_text":
ele = self.driver.find_element(By.PARTIAL_LINK_TEXT, locator)
wdw(self.driver, 10).until(ec.visibility_of_element_located(ele))
elif lc == "css_selector":
ele = self.driver.find_element(By.CSS_SELECTOR, locator)
wdw(self.driver, 10).until(ec.visibility_of_element_located(ele))
elif lc == "class_name":
ele = self.driver.find_element(By.CLASS_NAME, locator)
wdw(self.driver, 10).until(ec.visibility_of_element_located(ele))
else:
ele = self.driver.find_element(By.XPATH, locator)
wdw(self.driver, 10).until(ec.visibility_of_element_located(ele))
return ele
def send_key(self, lc, locator, value):
"""
找到元素,并完成输入
:param lc: 定位器,默认xpath定位
:param locator:
:param value:需要输入的字符串
:return:
"""
send = self.__find_element__(lc, locator)
send.send_keys(value)
刚刚改了一下,又出现了这种问题,send_keys 中 value 的值就是字符串
do_cf = ParseConFile()
URL = do_cf.get_locators_or_account('TESTURL', 'URL')
username = do_cf.get_locators_or_account('LoginAccountElements', 'username')
password = do_cf.get_locators_or_account('LoginAccountElements', 'password')
verification = do_cf.get_locators_or_account('LoginAccountElements', 'Verification')
login = do_cf.get_locators_or_account('LoginAccountElements', 'Login')
print(type(username))
print(username)
print(type(password))
print(password)
print(type(verification))
print(verification)
print(type(login))
print(login)
def login_busin(user, passwd):
img_name = "../img/scr.png"
br = WebKey(txt="gg")
# try:
br.open_url(URL)
br.sleep(tm=1)
br.__find_element__(lc='xpath', locator=username).send_keys(user)
br.__find_element__(lc='xpath', locator=password, ).send_keys(value=passwd)
read_image.screenshot(img_name)
text = read_image.ocr(img_name)
br.__find_element__(lc='xpath', locator=verification).send_keys(value=text)
br.click(lc='xpath', locator=login)
br.sleep(tm=1)
br.sleep(10)
br.close()
login_busin(user='ctz', passwd='admin123456?')
打印出来的是
<class 'tuple'>
('xpath', '//span[@class = "ant-input-affix-wrapper ant-input-affix-wrapper-lg"]/input[@placeholder=\'请输入帐户名\']')
<class 'tuple'>
('xpath', '//span[@class = "ant-input-affix-wrapper ant-input-affix-wrapper-lg"]/input[@placeholder=\'请输入密码\']')
<class 'tuple'>
('xpath', '//span[@class = "ant-input-affix-wrapper ant-input-affix-wrapper-lg"]/input[@placeholder=\'请输入验证码\']')
<class 'tuple'>
('xpath', "//button[@class='login_btn']")
Traceback (most recent call last):
File "D:/dingdang_project/DD_changtu/libs/login_business.py", line 44, in <module>
login_busin(user='ctz', passwd='admin123456?')
File "D:/dingdang_project/DD_changtu/libs/login_business.py", line 30, in login_busin
br.__find_element__(lc='xpath', locator=username).send_keys(user)
AttributeError: 'tuple' object has no attribute 'send_keys'