如何在python中将Selenium Webelelements转换为字符串列表 [英] How to convert selenium webelelements to list of strings in python

查看:1567
本文介绍了如何在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])

这篇关于如何在python中将Selenium Webelelements转换为字符串列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆