如何为span标签使用“ not(contains())”谓词编写Xpath

时间:2018-09-11 10:46:39

标签: selenium selenium-webdriver xpath

我想阅读有价格的产品,同样的方法想获得没有价格的产品的数量。我已经在XPath下面写过,但是不起作用

//div[@class='m-product-mini']//a[@class='m-product-mini-price ']//span[not(contains(text(),'$'))] 

没有价格元素的HTML:

<div class="m-product-mini-image">
<a href="#" class="btn btn-light btn-quickview no-mobile" style="opacity: 0;">Quick view</a> <a class="m-product-mini-price "><span></span> <span class="priceTag-discount"></span></a>
</div>

带有价格元素的HTML:

<div class="m-product-mini-image">
<a href="#" class="btn btn-light btn-quickview no-mobile">Quick view</a> <a class="m-product-mini-price "><span>$34.95</span> <span class="priceTag-discount"></span></a>
</div>

1 个答案:

答案 0 :(得分:0)

开头的类名称错误,div的类别为m-product-mini-image,但是您的xpath只是在寻找m-product-mini,即xpath:

try {
    $sql = "UPDATE table bla bla bla";
    $bind_param_arr = array();
    $stmt = $conn_p->prepare($sql);
    $stmt->execute($bind_param_arr);
    createLog(200);
    echo (json_encode(array('status' => 200, 'data' => 'Updated Successfully'));
} catch (PDOException $e) {
    createLog(500);
    echo (json_encode(array('status' => 500, 'data' => 'There is some error'));
}


function createLog($status)
{
    $log_file = "log.txt";

    $openfile = fopen($log_file, "a");
    if ( ! $openfile) {
        throw new Exception("unable to create log file");
    }
    $datetime = date("Y-m-d H:i:s a");
    fwrite($openfile, $datetime);
    fwrite($openfile, 'Bla bla bla');

    fwrite($openfile, "\n");
    fclose($openfile);
}

可以正常工作,但是这样做的问题是,它为每个条目找到2个span标签,因为您具有价格范围,该价格范围可以为空或其中有价格,然后具有class =的以下范围priceTag-discount。因此,在同时包含两个html代码段的页面上,它找到3个元素,而不是1个元素。因此,您需要删除其中一个span标签(也许在span标签中设置了包含价格的类),或者您将无法使用以下xpath

//div[@class='m-product-mini-image']//a[@class='m-product-mini-price ']//span[not(contains(text(),'$'))]
相关问题