简体   繁体   English

使用Selenium或xPath Python排除父元素的子元素

[英]Exclude child element of parent with selenium or xPath Python

I am stacked at the code, tried many ways and cannot find the right answer yet. 我被堆在代码上,尝试了许多方法,但找不到正确的答案。 This is the HTML code: 这是HTML代码:

<div class="description">
    <p>Text1</p>

    <p>Text1</p>

    <div class="excluding-class">
        <ul>
            <li>list1</li>
            <li>list2</li>
        </ul>
    </div>
</div>

I'm using Selenium and I have to pull some data from HTML code which has <div class="description"> . 我正在使用Selenium,必须从具有<div class="description"> HTML代码中提取一些数据。 But the child <div id="excluding-class"> is making me a problem so I want to exclude it by calling driver.get_element_by_class_name or driver.get_element_by_xpath 但是孩子<div id="excluding-class"> driver.get_element_by_class_name <div id="excluding-class">让我遇到了问题,因此我想通过调用driver.get_element_by_class_namedriver.get_element_by_xpath来排除它

Working code should export but without using <p> or something else: 工作代码应该导出,但不使用<p>或其他方式:

<div class="description">
    <p>Text1</p>

    <p>Text1</p>
</div<

Is there any way to do that? 有什么办法吗?

With only XPath 1.0 (the version most common in selenium webdrivers), there is no way to get the parent element who's innerHTML attribute will have the specified child element excluded. 仅使用XPath 1.0(Selenium Webdrivers中最常见的版本),无法获得其父元素的innerHTML属性将排除指定的子元素。 However, if removing the child element from the DOM is okay with you, the following is possible: 但是,如果可以从DOM中删除子元素,则可以执行以下操作:

driver.execute_script("document.getElementsByClassName('excluded-class').remove()")
driver.get_element_by_class_name("description").get_attribute("innerHTML")

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

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