Imran
Imran

Reputation: 21

AttributeError: 'list' object has no attribute 'text' in Selenium Python

Here is the code....

from selenium import webdriver

url = 'https://infobypokharelk.blogspot.com/'
driver = webdriver.Firefox()
driver.get(url)

row_count = len(driver.find_elements_by_xpath("//*[@id='post-body-6767393087210111064']/div[1]/table/tbody/tr"))
col_count = len(driver.find_elements_by_xpath("//*[@id='post-body-6767393087210111064']/div[1]/table/tbody/tr[1]/td"))

print("Number if Rows:",row_count)
print("Number of Columns",col_count)

first_part = "//*[@id='post-body-6767393087210111064']/div[1]/table/tbody/tr["
secound_part = "]/td["
third_part = "]"

for n in range(1,row_count+1):
    for m in range(1,col_count+1):
        final_path = first_part + str(n) + secound_part + str(m) + third_part
        table_data = driver.find_elements_by_xpath(final_path).text
        print(table_data,end = " ")
    print()

And the output is..

 File "tut_td.py", line 15, in <module>
    table_data = driver.find_elements_by_xpath(final_path).text
 AttributeError: 'list' object has no attribute 'text'

Upvotes: 1

Views: 10226

Answers (1)

AntiMatterDynamite
AntiMatterDynamite

Reputation: 1512

find_elements_by_xpath returns a list of elements not just one, so you need iterate over them with a loop or just grab the first one if you believe it to be the only one:

table_data = driver.find_elements_by_xpath(final_path)[0].text

Upvotes: 5

Related Questions