预期的字符串或标识符,但找到 <delimiter "/" at 8>

Expected string or identifier, but <delimiter "/" at 8> found

我有一个像这样的 Behat 步骤,它断言有一个 link 指向 URL 中带有 /videos/ 的东西:

And I must see at least 1 "*[href~=/videos/]"

实现如下:

/**
 * Asserts that at least X amount of Y exist
 *
 * @Then /^I must see at least (?P<amount>\d+) "([^"]*)"$/
 */
public function iMustSeeAtLeast($amount, $selector)
{
    $session = $this->getSession();
    $container = $this->getContainer() ?: $session->getPage();

    $elements = $container->findAll('css', $selector);
    $actual = count($elements);
    AssertionAbstract::assertGreaterThanOrEqual(
        (int)$amount,
        (int)$actual,
        'Expected at least ' . $amount . ' of ' . $selector . ', found only ' . $actual . ' on ' . $session->getCurrentUrl() . '.'
    );
}

例外情况是:

Exception 'Symfony\Component\CssSelector\Exception\SyntaxErrorException'
with message 'Expected string or identifier, but <delimiter "/" at 8> found.'
in vendor/symfony/css-selector/Exception/SyntaxErrorException.php:34

为什么我不能在步骤中加上斜杠?

不需要前面的*findAll会找到给定选择器的所有元素。
像这样格式化您的选择器:

[href~='/videos/']

a[href*='/artikel/']

如您收到的异常中所述,它不喜欢 /,您需要将其添加到 single/double 引号中。

您也可以尝试类似的方法:

a[href*=artikel]