标签:文本 drive 列表 find 数据 属性 输入 send span
# 标题
list_title = driver.find_elements_by_xpath(‘//*[@id="share-content"]/div/div[1]/ul/li/h3/a‘)
print(list_title)
# 用selenium + xpath 抓取网页数据,上述代码返回的是列表类型,打印出来是:
[<selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-1")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-2")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-3")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-4")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-5")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-6")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-7")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-8")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-9")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-10")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-11")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-12")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-13")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-14")>, <selenium.webdriver.remote.webelement.WebElement (session="35b2fa1b1832c956eb1f9d06cc3a1997", element="0.02111220449086315-15")>]
for i in list_title:
print(type(i))
显示结果:<class ‘selenium.webdriver.remote.webelement.WebElement‘>
抓取到的信息不能以中文形式显示出来,一时间陷入了怎么去编码解码解决这类型的代码,在网上找了好多方法去试,结果都报错,翻看笔记,原来这个方法很简单!!
for i in list_title:
print(i.text)
把列表遍历,.text方法即可得到想要的文本内容。与xpath中的text()又不一样。
1.从定位到的元素中获取数据
标签:文本 drive 列表 find 数据 属性 输入 send span
原文地址:https://www.cnblogs.com/lutt/p/10181799.html