0

AttributeError: 'list' object has no attribute 'text' . (Python Selenium) . Help me with suggestion or solution

###Here is the code from selenium import webdriver from selenium.webdriver.common.by import By import time driver = webdriver.Chrome() driver.get("https://learn.letskodeit.com/p/practice") rows = len(driver.find_elements(By.XPATH,'//*[@id="product"]/tbody/tr')) cols = len(driver.find_elements(By.XPATH,'//*[@id="product"]/tbody/tr[2]/td')) print("Number of rows: {}".format(rows)) print("Number of cols: {}".format(cols)) for i in range(rows): for j in range(cols): path = driver.find_elements(By.XPATH,'//*[@id="product"]/tbody/tr[" + i + "]/*[" + j + "]') print(path.text) time.sleep(5) driver.quit() #####Here is the output Number of rows: 4 Number of cols: 3 Traceback (most recent call last): File "td_text.py", line 14, in <module> print(path.text) AttributeError: 'list' object has no attribute 'text'

8th Jul 2018, 8:23 AM
IMRAN HOSEN
IMRAN HOSEN - avatar
9 Respostas
+ 6
#in #for path in path: #you should instead make it into for value in path: #and do this instead: print(value.text)
8th Jul 2018, 8:55 AM
Ahri Fox
Ahri Fox - avatar
+ 5
It's a list object. yknow, ["one","of","these"] So it can't convert it to text like that. Try printing the list (path) and see what pops up.
8th Jul 2018, 8:26 AM
Ahri Fox
Ahri Fox - avatar
+ 5
it's a list of elements. so try a for loop to get text off those. like: for element in path: print(element.text)
8th Jul 2018, 8:43 AM
Ahri Fox
Ahri Fox - avatar
+ 5
maybe the elements don't have text then. either way, I've addressed your problem: you can't turn a list of elements into text. assuming the elements have text, you have to extract them individually.
8th Jul 2018, 10:18 AM
Ahri Fox
Ahri Fox - avatar
+ 5
I said it twice earlier. a for loop. you said it didn't work, but didn't say the error. so I can't really help you further. other than to heed you that you followed my code, as your for loop version would do nothing.
8th Jul 2018, 10:21 AM
Ahri Fox
Ahri Fox - avatar
0
Now showing these..... <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-8")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-9")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-10")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-5")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-6")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-7")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-11")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-12")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-13")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-14")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-15")> <selenium.webdriver.remote.webelement.WebElement (session="75193e26ca3091822adfb429c95295a0", element="0.13991187731100374-16")>
8th Jul 2018, 8:32 AM
IMRAN HOSEN
IMRAN HOSEN - avatar
0
###I have done this from selenium import webdriver from selenium.webdriver.common.by import By import time driver = webdriver.Chrome() driver.get("https://learn.letskodeit.com/p/practice") rows = len(driver.find_elements(By.XPATH,'//*[@id="product"]/tbody/tr')) cols = len(driver.find_elements(By.XPATH,'//*[@id="product"]/tbody/tr[2]/td')) print("Number of rows: {}".format(rows)) print("Number of cols: {}".format(cols)) for i in range(rows): ii = str(i) for j in range(cols): jj = str(j) path = driver.find_elements(By.XPATH,"//*[@id='product']/tbody/tr[" + ii + "]/*[" + jj + "]") for path in path: print(value) time.sleep(5) driver.quit() ####output is.... the out put i post
8th Jul 2018, 8:50 AM
IMRAN HOSEN
IMRAN HOSEN - avatar
0
didn't work
8th Jul 2018, 9:03 AM
IMRAN HOSEN
IMRAN HOSEN - avatar
0
how?
8th Jul 2018, 10:18 AM
IMRAN HOSEN
IMRAN HOSEN - avatar