Claudaette
Claudaette

Reputation: 41

AttributeError: 'str' object has no attribute 'text' error

Got the: AttributeError: 'str' object has no attribute 'text' error for the following block:

 `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`

tried as well: if 'Version : 1.2.0.133' == i.text

Still not working.

print(version) is returning the correct value: Version : 1.2.0.133

But I cannot print the if value is true: print('Step 46. NAS version is displayed correctly - PASS')

Is spamming me with the else print FAIL value

Also if I use the .text for EC waiting will return an error as well.

Thank you

Upvotes: 1

Views: 4856

Answers (2)

Anna
Anna

Reputation: 124

I believe your version variable is a string, so when you write

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')

you're actually looping through each character in the string. So str(i) will be a single character which will never be equal to 'Version : 1.2.0.133', so you're always falling in the else statement, printing 'Step 46. NAS version is incorrect - Fail'. This is why you get that message on loop. As far as the error mentioned in the title of your question, I'm confused as to what the problem is, since you say that print(version) returns the correct value.

Upvotes: 5

Pepe Salad
Pepe Salad

Reputation: 223

I believe driver.find_element_by_id('com.project.PROJECT:id/version') returns a str object, which doesn't have a .text attribute. Try removing .text from the first line in your code.

Upvotes: 2

Related Questions