如何用代码获取元素?

Let's say we have a following html code:

<div>
  <p id="fruit">Apple</p>
</div>

How to grab #fruit and check its inner html via codeception?

It seems, that Codeception utilizes either executeJS or executeInSelenium(not recommended) in these kind of situations.

Here is an example using executeJS with JQuery:

<?php
  $fruitVal = $I->executeJS('return $(#fruit).val()');
?>

See more in: Codeception docs: executeJS


Turns out, that there is another, "more natural" way to handle this case:

Here is an example using grabAttributeFrom method:

<?php
  $fruitVal = $I->grabAttributeFrom('#fruit', 'innerHTML');
?>

See more in: Codeception docs: grabAttributeFrom