Saturday, September 24, 2016

How to switch between frames in selenium webdriver

Sometimes it happens that there is a link or a button which is located inside a frame in
website code. When selenium webdriver beginners try to click on it, they fail and had
hard time to understand and investigate why is button not clickable even if the locator
path is correct.
So, if you too are facing the same issue, you need to validate if your object is within a
frame. Follow the below steps:
1. Launch webpage in firefox browser which has the object
2. Install firebug and firepath add-ons . These add-ons are very helpful in selenium
3. Click on F12 key in keyboad to launch the firebug panel
4. Right click on the object which is within a frame and select "inspect in firepath"
5. Now verify the window name in top left in firebug panel ( See below screenshot )

6. If this option says anything other than 'Top Window' then your object is surely
within a frame.

So, to click an element within a frame, you first need to switch to frame. Selenium
webdriver provides method driver.switchTo().frame().
You can switch to a frame by it's name, number or ID. Just locate you frame in
console and enter the name/number/id in brackets.

The above steps will help you to click on an element which is located within a frame.
If you still face any issue, please write in comments section.

No comments:

Post a Comment