I am developing a Drupal 8 test which will check for a image's existence. The image will not be loaded while running the test and I want to catch that using xpath().

The HTML I am trying to search is,

<ul aria-labelledby="ckeditor-toolbar-group-aria-label-for-media" data-drupal-ckeditor-button-sorting="target" role="toolbar" class="ckeditor-buttons ckeditor-toolbar-group-buttons ui-sortable">
  <li class="ckeditor-button" data-drupal-ckeditor-button-name="Blockquote">
  <a aria-label="blockquote" title="blockquote" role="button" class="cke-icon-only cke_ltr" href="#"><span class="cke_button_icon cke_button__blockquote_icon">blockquote</span></a></li>
  <li class="ckeditor-button" data-drupal-ckeditor-button-name="DrupalImage">
    <a aria-label="Image" title="Image" role="button" href="#">
      <span class="cke_button_icon">
        <img title="Image" alt="" src="http://local/">

In this HTML you can see that I need to find the SRC in the tag. The SRC is invalid and it should be http://local/test.png. I am searching the HTML with $this->xpath('//span[@class="cke_button_icon"]/img[@src]'); but getting a blank array in return. How can I fetch this element?

closed as off-topic by kiamlaluno Aug 31 '14 at 2:27

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.


Perhaps you could try the following XPath: //span[@class="cke_button_icon"]/img/@src.


I'm not sure if you can directly get the property, I don't think so. What you want to do is get the img element and then the src property from it.

Something like this, untested:

$img = $this->xpath('//span[@class="cke_button_icon"]/img');
$src = $img[0]['src'];
  • //span[@class="cke_button_icon"]/img[@title="Image"]/@src -- gives me proper element in Firefox but not in the test case. Giving a try on your method also. I'm also online @ #drupal-contribute so let me know if you found any fix. Thanks! – Aneek Mukhopadhyay Aug 30 '14 at 8:56
  • We figured it out in IRC, the HTML wasn't actually there, it was dynamically built (CKEditor UI) with javascript. it didn't exist in the raw response. So you might want to close the question or accept the answer (if it would be there then the code above should work). – Berdir Aug 30 '14 at 17:15
  • Yes you are right Berdir. Both the answers by you and TerryCB are right in context of selecting the SRC. Just keeping this and giving both answers upvote. – Aneek Mukhopadhyay Aug 30 '14 at 17:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.