AttributeError:“str”对象没有属性“text”错误

Cla*_*tte 1 python selenium android attributes webdriver

收到以下块的 AttributeError: 'str' 对象没有属性 'text' 错误:

 `version = driver.find_element_by_id('com.project.PROJECT:id/version').text
  print(version)
  for i in version:
     if 'Version : 1.2.0.133' == str(i):
         print('Step 46. NAS version is displayed correctly - PASS')
     else:
          print('Step 46. NAS version is incorrect - Fail')
 time.sleep(2)
 pass`
Run Code Online (Sandbox Code Playgroud)

也尝试过: if 'Version : 1.2.0.133' == i.text

还是行不通。

print(version)返回正确的值:版本:1.2.0.133

但我无法打印if value is true: print('Step 46. NAS version is displayed correctly - PASS')

正在向我发送垃圾邮件else print FAIL value

另外,如果我使用.textfor EC 等待也会返回错误。

谢谢

Ann*_*nna 5

我相信你的version变量是一个字符串,所以当你写

for i in version:
     if 'Version : 1.2.0.133' == str(i):
         print('Step 46. NAS version is displayed correctly - PASS')
     else:
          print('Step 46. NAS version is incorrect - Fail')
Run Code Online (Sandbox Code Playgroud)

您实际上是在循环遍历字符串中的每个字符。因此str(i)将是一个永远不会等于“版本:1.2.0.133”的单个字符,因此您总是陷入 else 语句中,打印“步骤 46. NAS 版本不正确 - 失败”。这就是为什么您会循环收到该消息的原因。至于您的问题标题中提到的错误,我对问题是什么感到困惑,因为您说print(version)返回了正确的值。