Home > OS >  'list' object has no attribute 'text'
'list' object has no attribute 'text'

Time:10-14

How to fix this error 'list' object has no attribute 'text'

Davao14 = wd1.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[1]')
Davao15 = wd1.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[2]')
Davao16 = wd1.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[3]')
Davao17 = wd1.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[4]')
Davao18 = wd1.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[5]')
Davao19 = wd1.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[6]')
Davao20 = wd1.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[7]')
Davao21 = wd1.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[8]')

Cebu14 = wd2.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[1]')
Cebu15 = wd2.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[2]')
Cebu16 = wd2.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[3]')
Cebu17 = wd2.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[4]')
Cebu18 = wd2.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[5]')
Cebu19 = wd2.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[6]')
Cebu20 = wd2.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[7]')
Cebu21 = wd2.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[8]')

Manila14 = wd3.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[1]')
Manila15 = wd3.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[2]')
Manila16 = wd3.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[3]')
Manila17 = wd3.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[4]')
Manila18 = wd3.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[5]')
Manila19 = wd3.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[6]')
Manila20 = wd3.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[7]')
Manila21 = wd3.find_elements_by_xpath('/html/body/div[2]/div[4]/div/div[3]/div[1]/div[1]/div/div/div[8]')
data1 = [Davao14, Davao15, Davao16, Davao17, Davao18, Davao19, Davao20, Davao21]
data2 = [Cebu14, Cebu15, Cebu16, Cebu17, Cebu18, Cebu19, Cebu20, Cebu21]
data3 = [Manila14, Manila15, Manila16, Manila17, Manila18, Manila19, Manila20, Manila21]

for datas1 in data1:
  for datas2 in data2:
    for datas3 in data3:
      print("***DAVAO-CITY***\n\n")
      print(datas1.text)
      print("\n\n")
      print("***CEBU-CITY***\n\n")
      print(datas2.text)
      print("\n\n")
      print("***MANILA-CITY***\n\n")
      print(datas3.text)

OUTPUT:

***DAVAO-CITY***


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-96-63ba9131bde0> in <module>()
      7     for datas3 in data3:
      8       print("***DAVAO-CITY***\n\n")
----> 9       print(datas1.text)
     10       print("\n\n")
     11       print("***CEBU-CITY***\n\n")

AttributeError: 'list' object has no attribute 'text'

CodePudding user response:

datas1 is a list of strings while each data1 element is a string.
The same about the datas2 & data2 and datas3 & data3.
String element has no .text method.
List of string also has no .text method.
To print the all the lists of strings you should pass strings themselves directly to the print() method.
So instead of

data1 = [Davao14, Davao15, Davao16, Davao17, Davao18, Davao19, Davao20, Davao21]
data2 = [Cebu14, Cebu15, Cebu16, Cebu17, Cebu18, Cebu19, Cebu20, Cebu21]
data3 = [Manila14, Manila15, Manila16, Manila17, Manila18, Manila19, Manila20, Manila21]

for datas1 in data1:
  for datas2 in data2:
    for datas3 in data3:
      print("***DAVAO-CITY***\n\n")
      print(datas1.text)
      print("\n\n")
      print("***CEBU-CITY***\n\n")
      print(datas2.text)
      print("\n\n")
      print("***MANILA-CITY***\n\n")
      print(datas3.text)

It should be

data1 = [Davao14, Davao15, Davao16, Davao17, Davao18, Davao19, Davao20, Davao21]
data2 = [Cebu14, Cebu15, Cebu16, Cebu17, Cebu18, Cebu19, Cebu20, Cebu21]
data3 = [Manila14, Manila15, Manila16, Manila17, Manila18, Manila19, Manila20, Manila21]

for datas1 in data1:
  for datas2 in data2:
    for datas3 in data3:
      print("***DAVAO-CITY***\n\n")
      print(data1)
      print("\n\n")
      print("***CEBU-CITY***\n\n")
      print(data2)
      print("\n\n")
      print("***MANILA-CITY***\n\n")
      print(data3)

CodePudding user response:

Try like this once and confirm:

for d1,d2,d3 in zip(data1,data2,data3): # Iterate over lists

    for d11,d22,d33 in zip(d1,d2,d3): # iterate over list within lists
        
        print("***DAVAO-CITY***\n\n")
        print(d11.text)

        print("***CEBU-CITY***\n\n")
        print(d22.text)

        print("***MANILA-CITY***\n\n")
        print(d33.text)

CodePudding user response:

data1 = [Davao14, Davao15, Davao16, Davao17, Davao18, Davao19, Davao20, Davao21]

data1 is a list of list of web elements. what it means is, Davao14 is also a list. same for Davao15 and so on..

for datas1 in data1:
    for inside_list in datas1:
        print("***DAVAO-CITY***\n\n")
        print(inside_list.text)

You can similarly have different loops for data2 and data3

  • Related