Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to switch between frames in Selenium WebDriver using Java

I am using java with WebDriver.I have to switch between two frames. I have recorded the test case in selenium IDE and in that I got the values as selectFrame relative=top select Frame=middle Frame

But there is a problem it is not able to recognize the relative=top and middleFrame. How can I solve this problem in Selenium WebDriver with Java?

like image 734
Mallik Avatar asked Jun 04 '12 09:06

Mallik


People also ask

How do I switch to a specific frame in Selenium?

Another way to switch between frames in selenium is to pass the WebElement to the switchTo() command. Here the primary step is to find the iframe element and then pass it to the switch method.

How do I switch frames in Java?

To move back to the parent frame, you can either use switchTo(). parentFrame() or if you want to get back to the main (or most parent) frame, you can use switchTo(). defaultContent(); driver.

Which method is used to switch between frames?

Switching between different frames is done using the SwitchTo() command for frames. The SwitchTo frame command is used to switch to the intended frame (or iFrame). Frame id, frame name, and web element locator are used with the SwitchTo command for switching to the intended frame.

How do I switch between multiple iframes in Selenium?

To work with different nested iframes, we can use the method switchTo() to switch the frames using Index, Name, or Id & Web Element. Moreover, you can switch to immediate parent iframe using method switchTo(). parentFrame(). You can switch to the main web page from child iframe using method switchTo().


1 Answers

WebDriver's driver.switchTo().frame() method takes one of the three possible arguments:

  • A number.

    Select a frame by its (zero-based) index. That is, if a page has three frames, the first frame would be at index 0, the second at index 1 and the third at index 2. Once the frame has been selected, all subsequent calls on the WebDriver interface are made to that frame.

  • A name or ID.

    Select a frame by its name or ID. Frames located by matching name attributes are always given precedence over those matched by ID.

  • A previously found WebElement.

    Select a frame using its previously located WebElement.

Get the frame by it's id/name or locate it by driver.findElement() and you'll be good.

like image 125
Petr Janeček Avatar answered Sep 23 '22 00:09

Petr Janeček