繁体   English   中英

使用 xpath 在 selenium python 中选择文本节点

[英]selecting text node in selenium python with xpath

我想选择带有 selenium 和 xpath 的 hr 节点之后的某些文本。 但我不断收到 WebDriverException

这是我想从中提取文本的html 代码html 片段

我想得到的文字是:金融...商业决策简介

我使用了这个代码:

e = c.find_element_by_xpath("//div[@class='ajaxcourseindentfix']/hr/following-sibling::text()")

问题是我不断收到此异常

selenium.common.exceptions.WebDriverException: Message: TypeError: Expected an element or WindowProxy, got: [object Text] {}

我该怎么办?

在 selenium 中,您不能使用返回属性或文本节点的 XPath,因此不允许使用/text()语法。 如果您只想获取特定的子文本节点(节点)而不是完整的文本内容(由text属性返回),您可能会执行复杂的 JavaScript

我试图从这个问题实施解决方案,它似乎有效,因此您可以应用以下代码来获取所需的文本节点:

driver.execute_script("""var el = document.createElement( 'html' );
                         el.innerHTML = '<div>' + document.querySelector('div.ajaxcourseindentfix').innerHTML.split('<hr>')[1];
                         return el.querySelector( 'div' ).textContent;""")

输出是

Introduction to financial and managerial accounting theory and practice with emphasis on the role of accounting information in business decisions.

HTML 有 3 种类型的节点:元素/属性/文本节点,Selenium 的 findElement 需要元素节点作为返回值。

在您的 XPath text()中将选择 Text Node,这就是您收到该错误的原因。

但是我们可以使用 javascript 与 Text Node 进行交互。

script = """
    var text = '';

    var childNodes = arguments[0].childNodes; // child nodes includes Element and Text Node

    childNodes.forEach(function(it, index){
      if(it.nodeName.toUpperCase() === 'HR') { // iterate until Element Node: hr
        text = childNodes[index+1].textContent; 
        // get the text content of next Child Node of Element Node: hr
      }
    });
    return text;
"""
ele = driver.find_elements_by_css_selector("div.ajaxcourseindentfix")
text = driver.execute_script(script, ele)
print text

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM