首页 > 解决方案 > 如何使用 xpath 获得硒中的非罢工价格金额?

问题描述

这是我试图自动化的网页的网址:

http://www.qaclickacademy.com/courses-description.php

我想使用 Selenium 和 XPath 定位器获取非删除线价格(目前为 20.00 美元)的价值。

包含我感兴趣的元素的 HTML 标记片段是:

<div class="course row" data-scroll-reveal=""
     style="-webkit-transform: translatey(24px);transform: translatey(24px);opacity: 0;-webkit-transition: -webkit-transform 0.66s ease-in-out 0s,  opacity 0.66s ease-in-out 0s;transition: transform 0.66s ease-in-out 0s,  opacity 0.66s ease-in-out 0s;-webkit-perspective: 1000;-webkit-backface-visibility: hidden;"
     data-scroll-reveal-initialized="true">
    <div class="col-sm-4">
        <a href="course-detail.php?id=130&amp;t=websecurity-testing-for-beginners-qa-knowledge-to-next-level">
            <img src="/courses-description.php?show=130" alt="websecurity-testing-for-beginners-qa-knowledge-to-next-level" class="img-responsive" width="186" height="123">
        </a>
    </div>
    <div class="col-sm-8">
        <div class="row">
            <div class="col-md-9 col-sm-8">
                <h3>
                    <a href="course-detail.php?id=130&amp;t=websecurity-testing-for-beginners-qa-knowledge-to-next-level">
                        WebSecurity Testing for Beginners-QA knowledge to next level
                    </a>
                </h3>
                <div class="meta">
                    <span><i class="fa fa-user"></i><a href="#">Rahul Shetty</a></span>
                    <span><i class="fa fa-file-text"></i>60 Lessons</span>
                    <span><i class="fa fa-folder"></i><a href="#">Penetration testing</a></span>
                </div>
            </div>
            <div class="col-md-3 col-sm-4 price">
                <del style="font-size:15px;color:#aaa">$ 85.00</del>
                <br>
                $ 20.00
            </div>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <p class="course-desc">
                    Course Launch Date : Aug 30th 2015 -Its Time to Protect our Websites from Security Attacks This Tutorial will give all the weapons you needed to investigate and
                    unlock the Security Holes in the Web applicationCourse lectures are conceptually driven with root level explanations and bring you to the level where you can
                    bring out the security bugsCourse Contents: Basics of Security Testing...
                    <br>
                    <a href="course-detail.php?id=130&amp;t=websecurity-testing-for-beginners-qa-knowledge-to-next-level">
                        Read More
                        <i class="fa fa-angle-right"></i>
                    </a>
                </p>
            </div>
        </div>
    </div>
    <div class="col-md-12">
        <hr>
    </div>
</div>

我尝试了很多方法,但到目前为止我一直无法找到解决方案

标签: javascriptseleniumxpathwebdriverwaitxpath-1.0

解决方案


这是python中的方法,它只会得到价格(20.00美元)。注意:这不适用于您是否有定价的两种情况。

def get_text_exclude_children(element):
    return driver.execute_script(
        """
        var parent = arguments[0];
        var child = parent.firstChild;
        var textValue = "";
        while(child) {
            if (child.nodeType === Node.TEXT_NODE)
                    textValue += child.textContent;
                    child = child.nextSibling;
        }
        return textValue;""",
        element).strip()

如何使用这里。

element = driver.find_element_by_xpath("(//div[@class='col-md-3 col-sm-4 price'])[1]")
price = get_text_exclude_children(element)

推荐阅读