如何在标签下获取文本 [英] How to get the text under the tag

查看:56
本文介绍了如何在标签下获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取标签下的文本

I'm trying to get the text under the tag

我尝试了几种不同的选择:

I tried several different options:

dneyot=driver.find_elements_by_xpath("//*[starts-with(@id, 'popover-')]/text()")
dneyot=driver.find_elements_by_xpath("//*[starts-with(@id, 'popover-')]/b[1]/text()")

我的一段代码:

dneyot=driver.find_elements_by_xpath("//*[starts-with(@id, 'popover-')]/text()")
for spisok in dneyot:
    print("Период показов >3 дней", spisok.text)

UPD:我在浏览器中使用以下命令找到了所需的项目:

UPD: I find the items I need in the browser using :

//*[starts-with(@id, 'popover-')]/text()[1]

但出现错误

    selenium.common.exceptions.InvalidSelectorException:
Message: invalid selector: The result of the xpath expression "//*[starts-with(@id, 'popover-')]/text()[1]" is: [object Text]. It should be an element.

推荐答案

如果要获取除< b> 节点文本之外的文本,则需要使用以下XPath:

If you want to get that text excluding the <b> node text then you need to use the below XPath:

//div[starts-with(@id, 'popover-')]

将标识div节点,然后使用 find_elements_by_xpath()方法,您可以从div节点检索所有文本.请尝试以下代码:

which will identify the div node and then by using find_elements_by_xpath() method, you can retrieve all the text from div node. Try the code below:

elements = driver.find_elements_by_xpath("//div[starts-with(@id, 'popover-')]") 
for element in elements:
    print(element.text)

更新:

我怀疑上述方法可能无法正常工作,我们可能无法使用常规方法识别/获取数据-在这种情况下,您需要使用JavaScriptExecutor来获取如下数据:

I suspect, the above method may not work and we may not be able to identify/get that data using the normal methods - in that case you need to use JavaScriptExecutor to get the data like below :

driver = webdriver.Chrome('chromedriver.exe')
driver.get("file:///C:/NotBackedUp/SomeHTML.html")

xPath = "//div[starts-with(@id, 'popover-')]"
elements = driver.find_elements_by_xpath(xPath)
for element in elements:
    lenght = int(driver.execute_script("return arguments[0].childNodes.length;", element));
    for i in range(1, lenght + 1, 1):
        try:
            data = str(driver.execute_script("return arguments[0].childNodes["+str(i)+"].textContent;", element)).strip();
            if data != None and data != '':
                print data
        except:
            print "=> Can't print some data..."

由于您的网站是用英语以外的其他语言编写的,因此您可能无法打印/获取某些数据.

As your site is written in some other language other than English, you may not able to print/get some data.

要获取特定的子节点数据,您需要执行以下操作:

For getting specific child nodes data, you need to do like below :

from selenium import webdriver
driver = webdriver.Chrome('chromedriver.exe')
driver.get("file:///C:/NotBackedUp/SomeHTML.html")

xPath = "//div[starts-with(@id, 'popover-')]"
elements = driver.find_elements_by_xpath(xPath)
for element in elements:
    # For print b1 text
    b1Text = driver.execute_script("return arguments[0].childNodes[2].textContent", element);
    print b1Text

    # For printing b2 text
    b2Text = driver.execute_script("return arguments[0].childNodes[6].textContent", element);
    print b2Text

print("=> Done...")

我希望它能对您有所帮助...

I hope it helps...

这篇关于如何在标签下获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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