【问题标题】:How to get the non-strike price amount in selenium using xpath?如何使用 xpath 获得硒中的非罢工价格金额?
【发布时间】:2019-03-29 11:33:41
【问题描述】:

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

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

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

包含我感兴趣的元素的 HTML 标记的 sn-p 是:

<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>

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

【问题讨论】:

  • 试试这个它会起作用 //del[contains(text(),'$ 85.00')]
  • 输出应该是“$ 20.00”。价格是动态的。它会经常改变......
  • 哪一个,有4个
  • 首个产品价格“$20.00”
  • 您使用哪种语言绑定? Java/Python?

标签: javascript selenium xpath webdriverwait xpath-1.0


【解决方案1】:

这是 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)

【讨论】:

    【解决方案2】:

    非删除线价格,即文本 $ 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
      

    【讨论】:

      【解决方案3】:
      xpath=//section[@id='content']/div/div/div/div/div/div[2]/div/div[2]
      

      【讨论】:

      • 这两个值都反映了 $85.00 $20.00。需要获取非罢工值
      • 你可以尝试在末尾添加另一个 [2] 吗?
      • 不打算做任何事情,OP 正在尝试查找文本节点而不是元素
      【解决方案4】:

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

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

      //*[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 的定位器(因为它没有找到元素,它会找到文本节点)。

      修复它的最佳方法是提出一个错误并让开发人员将未穿透的数量放入它自己的元素中(例如用&lt;span&gt; 包装它)。

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

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

      String elementHTML 将包含以下内容:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-19
        • 1970-01-01
        • 2018-04-22
        相关资源
        最近更新 更多