如何在python中将Selenium Webelelements转换为字符串列表
我已经从scopus网站上收集了强制性数据.我的输出已保存在名为文档"的列表中.当我为该列表的每个元素使用类型方法时,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'>"
为了解决这个问题,我使用了如下的文本方法:
document=driver.find_elements_by_tag_name('td')
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])
将返回空白.
我该怎么办?
print (x[2])
will return white space.
What should I do?
使用下面的代码行:
document=driver.find_elements_by_tag_name('td')
并在控制台上看到的输出为:
and see the output on Console as :
"<class'selenium.webdriver.firefox.webelement.FirefoxWebElement'>"
这是预期的行为,因为 Selenium
打印与您的搜索条件匹配的 Nodes
的参考.
This is the expected behavior as Selenium
prints the reference of the Nodes
matching your search criteria.
根据您的Code Attempt
来打印掉white spaces
的文本,您可以使用以下代码块:
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])