5

I understand that you can get the text from an element, like this:

$text = $browser->text('.selector');

My question is: How can you get the raw HTML content? E.g. the following doesn't work

$text = $browser->html('.selector');

E.g. if the element is:

<div class='selector'><p>Hello</p></div>

I'd like to get the following as the output (with the < p> tags):

<p>Hello</p>

Thanks

user6122500
  • 892
  • 1
  • 15
  • 31

3 Answers3

12

To get the source code of your element you should use the element() method together with the getAttribute() like this:

$html = $browser->element('.selector')->getAttribute('innerHTML');
// now in $html you have HTML inside .selector element
Christopher Dosin
  • 1,301
  • 9
  • 15
  • 1
    Works perfectly. Thanks! – user6122500 May 08 '19 at 00:02
  • Try these 2 links too which are extremely useful for me [github](https://github.com/laravel/dusk/issues/303#issuecomment-311656596) and [forum](https://www.5balloons.info/understanding-selectors-laravel-dusk-browser-testing) – Phantom1412 Aug 02 '20 at 10:10
2

update laravel 8.75.0 and laravel dusk 6.19

$browser->element('yourElement')->getDomProperty('innerHTML');
0

I couldn't find a method to get the inner HTML, but you can use getText() to extract inner Text.

<div class='selector'><p>Hello</p></div>
<?php 
  $innerText = $browser->element('.selector p')->getText();
  // $innerText = 'Hello'
?>

Tried on laravel/framework 9.0.2; laravel/dusk 6.22.0; php-webdriver/webdriver 1.9.0;

apzeero
  • 43
  • 1
  • 5