【问题标题】:xpath for no tag elements无标签元素的 xpath
【发布时间】:2021-10-27 20:34:26
【问题描述】:

<span class="uniqueid">abc</span>"efg"

如何使用 XPath 获取 efg 的值?我可以通过//span[@class="uniqueid"]得到abc

【问题讨论】:

    标签: python selenium xpath


    【解决方案1】:

    您可以在 xpath 表达式中使用 following-sibling::text() 谓词。

    这是一个例子:

    from lxml import html
    
    hstr = '<div><span class="uniqueid">abc</span>"efg"</div>'
    
    h = html.fromstring(hstr)
    h.xpath('//span[@class="uniqueid"]/following-sibling::text()')
    # returns:
    ['"efg"']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-10
      • 2016-10-30
      • 1970-01-01
      • 2013-06-23
      相关资源
      最近更新 更多