本文介绍了waitForSelector传递,但assertExists对同一个选择器失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我调用下面的函数时, waitForSelector 传递给'selector',但 assertExists 。如何实现?

When I call the following function, waitForSelector passes for 'selector', but assertExists fails for the same selector. How is it possible?

casper.waitForSelector(selector, function() {
    casper.test.assertExists(selector, sectionName + " opened up successfully.");
}, function() {
    casper.test.fail(sectionName + " did not load in given time");
}, max_timeout);

是用:nth-​​child 选择器重现问题的完整示例。

Here is a complete example to reproduce the issue with an :nth-child selector.

推荐答案

这是已知的错误(请参阅,)在WebKit的Qt4分支(从2010年)。当:nth-​​child():nth-​​of-type()时,会使用CSS3选择器。第二次运行选择器时,返回不同的结果(大部分时间 null )。唯一已知的解决方法是使用XPath表达式而不是CSS3选择器。这个bug在PhantomJS 2中修复,因为它使用WebKit的Qt5分支(版本538.1)。

This is a known bug (see #11632, #11737) in the Qt4 fork of WebKit (from 2010). It happens when :nth-child() or :nth-of-type() CSS3 selectors are used. The second time the selector is run, it returns a different result (most of the time null). The only known workaround is to use XPath expressions instead of CSS3 selectors. This bug is fixed in PhantomJS 2 as it uses a Qt5 fork of WebKit (version 538.1).

这是一个最小的脚本来重现(修改自):

This is a minimal script to reproduce the issue on http://example.com (modified from here):

var casper = require('casper').create(),
    x = require('casper').selectXPath;

casper.start('http://example.com', function() {
    var selector = 'p:nth-child(3) > a',
        xpSelector = '//*[local-name()="p" and position()=3]/a';
    var first = this.exists(selector);
    var second = this.exists(selector);
    if(first !== second) {
        console.error('Expected First selector to equal the Second');
    } else {
        console.log('Passed');
    }
    first = this.exists(x(xpSelector));
    second = this.exists(x(xpSelector));
    if(first !== second) {
        console.error('Expected First selector to equal the Second');
    } else {
        console.log('Passed');
    }
}).run();

输出:


Expected First selector to equal the Second
Passed

标记是:

<div>
    <h1>text</h1>
    <p>text</p>
    <p><a href="url">text</a></p>
</div>

XPath表达式看起来有点尴尬,因为它直接再现了CSS选择器的预期行为。通常会写 // p [2] / a

The XPath expression looks a little awkward, because it directly reproduces the intended behavior of the CSS selector. Normally one would write //p[2]/a.

这篇关于waitForSelector传递,但assertExists对同一个选择器失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:17