如何使用 xpath 获取 selenium 中的非行使价金额?

How to get the non-strike price amount in selenium using xpath?

这是我要自动化的网页的 url:

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>

我试过很多方法,但至今没找到解决办法

这是 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)
xpath=//section[@id='content']/div/div/div/div/div/div[2]/div/div[2]

如果不提供更多详细信息,很难知道如何回答您的问题,页面上有多个项目有删除线/未删除线的金额。

对于通用解决方案,您可以使用以下内容,它将匹配多个有价格的块。如果您想要更有针对性的东西,您将需要一个稍微复杂的定位器来指定项目和价格块。如果您只想要页面上的第一个价格块,这将起作用:

//*[contains(@class,'price')]

这与下面的 CSS 选择器相同(更简单)

.price

但是加价会给您带来麻烦,看起来像这样:

<div class="col-md-3 col-sm-4 price">
    <del style="font-size:15px;color:#aaa">$ 85.00</del>
    <br>
    $ 20.00
</div>

在严格的 XPath 中,您可以使用像这样的 XPath 来做到这一点:

//*[contains(@class,'price')]/text()

但是 Selenium 不允许您将文本节点绑定到 WebElement。这意味着虽然上面的 Xpath 将直接在浏览器中工作,但它不能用作查找 WebElement 的定位器(因为它没有找到元素,而是找到文本节点)。

修复它的最佳方法是提出错误并让开发人员将未删除的金额放入其自己的元素中(例如用 <span> 包装它)。

作为一个 hacky 解决方法,您可以尝试像这样获取元素的内部 HTML:

WebElement price = driver.findElement(By.cssSelector(".price"));
String elementHTML = price.getAttribute("innerHTML");

字符串元素HTML 将包含以下内容:

<del style="font-size:15px;color:#aaa">$ 85.00</del>
<br>
$ 20.00

然后您需要解析字符串以去除前两行(尽管这不是一个好的或可靠的解决方案)。

非删除线价格,即文本 $ 20.00 是一个 文本节点 并且要检索文本,您可以使用以下解决方案:

  • Java解法:

    WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='course row']//div[contains(@class, 'price')]")));
    String myText = ((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", element).toString();
    System.out.println(myText);
    
  • 控制台输出:

    $ 20.00