Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用xpath获取selenium中的非执行价格金额?_Javascript_Selenium_Xpath_Webdriverwait_Xpath 1.0 - Fatal编程技术网

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

Javascript 如何使用xpath获取selenium中的非执行价格金额?,javascript,selenium,xpath,webdriverwait,xpath-1.0,Javascript,Selenium,Xpath,Webdriverwait,Xpath 1.0,这是我正在尝试自动化的网页的url: 我想使用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-t

这是我正在尝试自动化的网页的url:

我想使用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>

60课
$ 85.00

$ 20.00

课程发布日期:2015年8月30日-是时候保护我们的网站免受安全攻击了。本教程将为您提供所有必要的武器,以便您进行调查和评估 解锁Web应用程序中的安全漏洞课程讲座从概念上讲是由根级别的解释驱动的,并将您带到您可以理解的级别 带出安全漏洞分析内容:安全测试基础。。。



我已经尝试了很多方法,但是到目前为止我还没有找到一个解决方案

这里是python中的方法,它只会得到20美元的价格。 注意:这将不适用于您是否降价的两种情况

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)

如果没有更详细的信息,很难知道如何回答您的问题,页面上有多个项目具有通过/不通过罢工的金额

对于一个通用的解决方案,您可以使用以下方法,它将匹配多个具有价格的块。如果你想要更具针对性的东西,你需要一个稍微复杂一点的定位器来指定物品和价格块。如果您只需要页面上的第一个价格块,则可以:

//*[contains(@class,'price')]
这与下面的CSS选择器相同(不太复杂)

然而,加价会给您带来一个问题,它看起来是这样的:

<div class="col-md-3 col-sm-4 price">
    <del style="font-size:15px;color:#aaa">$ 85.00</del>
    <br>
    $ 20.00
</div>
WebElement price = driver.findElement(By.cssSelector(".price"));
String elementHTML = price.getAttribute("innerHTML");
但是,Selenium不允许将文本节点绑定到WebElement中。这意味着,尽管上面的Xpath将直接在浏览器中工作,但它不会用作查找WebElement的定位器(因为它找不到元素,而是查找文本节点)

修复它的最佳方法是引发一个bug,并让开发人员将未命中的金额放入自己的元素中(例如,用
将其包装)

作为一项黑客工作,您可以尝试获取元素的内部HTML,如下所示:

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

$ 20.00

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

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

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);
  • 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
    

试试这个,它会起作用的//del[contains(text(),'$85.00')]输出应该是“$20.00”。价格是动态的。它会经常改变…哪一个,有4首产品价格“$20.00”您使用哪种语言绑定?Java/Python?这两个值都反映了$85.00$20.00。需要获取非strike值您可以尝试在末尾添加另一个[2]吗?不做任何事情,OP正在尝试查找文本节点而不是元素