Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium php-webdriver check attribute after click

<a class="lnk" href="http://www.google.com">Go Google</a>

When click on this link, another css class 'loading' will append. How do we test this before redirecting to google.com.

$element = $driver->findElement(WebDriverBy::className('lnk'));
$element->click();

Is there any way to check the class attribute contains 'loading' before redirecting to destination?

like image 482
Habeeb Avatar asked Mar 29 '26 20:03

Habeeb


1 Answers

You can use JavaScript to edit the href attribute and add it timeout

$element = $driver->findElement(WebDriverBy::className('lnk'));
$href = $element->getAttribute('href');
$script = "javascript:setTimeout( function() { window.location = {$href} }, 5000 );"
$driver->executeScript("arguments[0].setAttribute('href', arguments[1]);", $element, $script);

Now yo have enough time to check if class attribute contains loading

$element = $driver->findElement(WebDriverBy::className('lnk'));
$class = $element->getAttribute('class');
if (strpos($class, 'loading') !== false) { }
like image 157
Guy Avatar answered Mar 31 '26 11:03

Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!