Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert selenium webelelements to list of strings in python

I have gathered obligatory data from the scopus website. my outputs have been saved in a list named "document". when I use type method for each element of this list, the python returns me this class:

"<class'selenium.webdriver.firefox.webelement.FirefoxWebElement'>" 

In continius in order to solve this issue, I have used text method such this: document=driver.find_elements_by_tag_name('td')

for i in document:  
    print i.text

So, I could see the result in text format. But, when I call each element of the list independently, white space is printed in this code:

x=[]
for i in document:
     x.append(i.text)

print (x[2]) will return white space. What should I do?

like image 979
Hamed Baziyad Avatar asked Sep 03 '25 04:09

Hamed Baziyad


1 Answers

As you have used the following line of code :

document=driver.find_elements_by_tag_name('td')

and see the output on Console as :

"<class'selenium.webdriver.firefox.webelement.FirefoxWebElement'>" 

This is the expected behavior as Selenium prints the reference of the Nodes matching your search criteria.

As per your Code Attempt to print the text leaving out the white spaces you can use the following code block :

x=[]
document = driver.find_elements_by_tag_name('td')
for i in document :
    if (i.get_attribute("innerHTML") != "null") :
    x.append(i.get_attribute("innerHTML"))
print(x[2])
like image 125
undetected Selenium Avatar answered Sep 06 '25 00:09

undetected Selenium