Search code examples
phpseleniumselenium-webdriverwebdrivercodeception

Create a list of clickable nodes (web elements) in Codeception test (webdriver)


I am using the WebDriver module in Codeception acceptance tests.

In a test I need to create a list of nodes (web elements) from a web page and click each node in a foreach loop. Nodes are main navigation menu items. See screen shot.

screen shot

I've tried to use a built-in method called grabMultiple("//nav[@class='nav-container']/ul/li/a") but it returns a list of names ("Ready to Wear", etc.) and the click method can't click such elements of this list.

Is there any way in Codeception to create a list of nodes which are clickable?

When I use following code:

$itemsOfMainNav = $I->grabMultiple("//nav[@class='nav-container']/ul/li/a");

foreach($itemsOfMainNav as $item){
    $I->click($item);
}

I'm getting error:

1) Failed to check main categories are reachable in 101_CategoryFlowCept (tests\acceptance\101_CategoryFlowCept.php)

Step I click "READY TO WEAR" Fail Link or Button or CSS or XPath element with 'READY TO WEAR' was not found.

Scenario Steps:

  1. $I->click("READY TO WEAR")
  2. $I->grabMultiple("//nav[@class='nav-container']/ul/li/a")
  3. $I->wait(1)

Solution

  • After suggestion of Naktibalda I've created following code and it works for me:

    $mainNavLinks = $I->grabMultiple("//nav[@class='nav-container']/ul/li/a", 'href'); 
    foreach ($mainNavLinks as $link) { 
        $I->amOnUrl($link); 
    }