@@ -2521,37 +2521,42 @@ public function switchToWindow($name = null)
25212521 *
25222522 * Example:
25232523 * ``` html
2524- * <iframe name="another_frame" src="http://example.com">
2524+ * <iframe name="another_frame" id="fr1" src="http://example.com">
25252525 *
25262526 * ```
25272527 *
25282528 * ``` php
25292529 * <?php
2530- * # switch to iframe
2530+ * # switch to iframe by name
25312531 * $I->switchToIFrame("another_frame");
2532+ * # switch to iframe by CSS or XPath
2533+ * $I->switchToIFrame("#fr1");
25322534 * # switch to parent page
25332535 * $I->switchToIFrame();
25342536 *
25352537 * ```
25362538 *
2537- * @param string|null $name
2539+ * @param string|null $locator ( name, CSS or XPath)
25382540 */
2539- public function switchToIFrame ($ name = null )
2541+ public function switchToIFrame ($ locator = null )
25402542 {
2541- if (is_null ($ name )) {
2543+ if (is_null ($ locator )) {
25422544 $ this ->webDriver ->switchTo ()->defaultContent ();
25432545 return ;
25442546 }
25452547 try {
2546- $ this ->webDriver -> switchTo ()-> frame ( $ name );
2548+ $ els = $ this ->_findElements ( " iframe[ name=' $ locator '] " );
25472549 } catch (\Exception $ e ) {
2550+ $ this ->debug ('Failed to find locator by name: ' . $ e ->getMessage ());
2551+ }
2552+ if (!is_array ($ els ) || !count ($ els )) {
25482553 $ 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 ' );
25542558 }
2559+ $ this ->webDriver ->switchTo ()->frame ($ els [0 ]);
25552560 }
25562561
25572562 /**
0 commit comments