'Laravel Dusk: How to get HTML from element?
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
Solution 1:[1]
update laravel 8.75.0 and laravel dusk 6.19
$browser->element('yourElement')->getDomProperty('innerHTML');
Solution 2:[2]
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;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | apzeero |
