mks mks - 2 months ago 16
PHP Question

How to get the Xpath in behat selenium

Trying to find Xpath in a page.
here's my code.

$session = $this->getSession();
$found = $session->getPage()->findAll('xpath', '//meta[@name ="description"]/@content');
if (is_null($found)) {
throw new \Exception(sprintf("Could not find meta %s='%s' with '%s'",));

giving error

invalid selector: The result of the xpath expression
"//html//meta[@name ="description"]/@content" is: [object Attr]. It
should be an element.
(Session info: chrome=51.0.2704.103)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5
x86_64) (WARNING: The server did not provide any stacktrace


Using '@content' will return the attribute content, this might not return a valid selector when Behat is translating to xpath.

If you need to specify that attribute you should use it like this:

//meta[@name ="description"][@content]

Also please note that findAll returns array and not a single element, you can use find to find the first element matching the selector given.

If you need to get a value of an attribute or to get the text you can use methods like getAttribute('attribute_name') or getText().

In your case you can have something like: