@@ -2521,37 +2521,42 @@ public function switchToWindow($name = null)
2521
2521
*
2522
2522
* Example:
2523
2523
* ``` html
2524
- * <iframe name="another_frame" src="http://example.com">
2524
+ * <iframe name="another_frame" id="fr1" src="http://example.com">
2525
2525
*
2526
2526
* ```
2527
2527
*
2528
2528
* ``` php
2529
2529
* <?php
2530
- * # switch to iframe
2530
+ * # switch to iframe by name
2531
2531
* $I->switchToIFrame("another_frame");
2532
+ * # switch to iframe by CSS or XPath
2533
+ * $I->switchToIFrame("#fr1");
2532
2534
* # switch to parent page
2533
2535
* $I->switchToIFrame();
2534
2536
*
2535
2537
* ```
2536
2538
*
2537
- * @param string|null $name
2539
+ * @param string|null $locator ( name, CSS or XPath)
2538
2540
*/
2539
- public function switchToIFrame ($ name = null )
2541
+ public function switchToIFrame ($ locator = null )
2540
2542
{
2541
- if (is_null ($ name )) {
2543
+ if (is_null ($ locator )) {
2542
2544
$ this ->webDriver ->switchTo ()->defaultContent ();
2543
2545
return ;
2544
2546
}
2545
2547
try {
2546
- $ this ->webDriver -> switchTo ()-> frame ( $ name );
2548
+ $ els = $ this ->_findElements ( " iframe[ name=' $ locator '] " );
2547
2549
} catch (\Exception $ e ) {
2550
+ $ this ->debug ('Failed to find locator by name: ' . $ e ->getMessage ());
2551
+ }
2552
+ if (!is_array ($ els ) || !count ($ els )) {
2548
2553
$ this ->debug ('Iframe was not found by name, locating iframe by CSS or XPath ' );
2549
- $ frames = $ this ->_findElements ($ name );
2550
- if (!count ($ frames )) {
2551
- throw $ e ;
2552
- }
2553
- $ this ->webDriver ->switchTo ()->frame ($ frames [0 ]);
2554
+ $ els = $ this ->_findElements ($ locator );
2555
+ }
2556
+ if (!count ($ els )) {
2557
+ throw new ElementNotFound ($ locator , 'Iframe ' );
2554
2558
}
2559
+ $ this ->webDriver ->switchTo ()->frame ($ els [0 ]);
2555
2560
}
2556
2561
2557
2562
/**
0 commit comments