Laravel Dusk: How to get HTML from element?
Asked Answered
F

3

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

Festination answered 7/5, 2019 at 9:58 Comment(0)
I
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
Inman answered 7/5, 2019 at 15:35 Comment(2)
Works perfectly. Thanks!Festination
Try these 2 links too which are extremely useful for me github and forumImproper
D
4

update laravel 8.75.0 and laravel dusk 6.19

$browser->element('yourElement')->getDomProperty('innerHTML');
Diffraction answered 25/12, 2021 at 17:31 Comment(0)
B
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;

Bessiebessy answered 14/2, 2022 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.