laravel-5xpathcss-selectorsdomcrawler

Symfony \ Component \ CssSelector \ Exception \ SyntaxErrorException cssselector to xpath in laravel


I have following html select element, select element's attribute name containing square bracket and need to filter using domcrawler but unfortunately, CssSelectorConvertor() cannot convert cssselector to xpath because of square bracket in the name attribute.

HTML code of another page:

<select name="get_result[our_school_dis]">
    <option value="a">Result 1</value>
</select>

code in laravel controller:

$crawler = $client->request('GET', 'http://anotherpage.com/abc');
$converter = new CssSelectorConverter();
$converter->toXPath('select[name=result[our_school_dis]]');

It throws error as :

Symfony \ Component \ CssSelector \ Exception \ SyntaxErrorException
Expected "]", but <delimiter "[" at 22> found.

looking for appropriate solution


Solution

  • For attribute value that contains special character, use quotes in your CSS selector :

    $converter->toXPath('select[name="result[our_school_dis]"]');