3

I'm creating some test cases using Selenium WebDriver in C# for a web application. That web application uses different frames for different elements and those frames are not having any Name and ID. After doing some googling I found that frames can be referred with the number too, starting from '0'. So, I used the same; as some of the elements which were throwing error earlier are now working after I used

driver.SwitchTo().Frame(1);

I used it randomly and it worked, but when I use driver.SwitchTo().Frame(0); for referring to the earlier frame it didn't work, for this I have to use driver.SwitchTo().DefaultContent(); for going back to the last frame.

Questions:

  1. Is this the correct method of handling frames without Name and ID?
  2. How to provide correct numbering to frames? Lets say there are 7-8 frames then, how I know which frame is at 0, 1 , 2 ...etc
  3. Why this reverse method of handling frame is not working?

3 Answers 3

1

You can find an frame and then switch to it.

WebElement frame = driver.findElement(By.xpath(<your frame xpath>));
driver.switchTo().frame(frame);
0

For unnamed iFrames I think I would use the SRC attribute, since this should be unique:

driver.switchTo().frame(
  driver.findElement(By.XPath("//iframe[contains(@src,'<the contents of the src here')]"))
);

I think the frame numbers may change each run depending on the order of loading of the browser. Not sure when it gets its internal ID, I would not depend on it for a page with 7+ iFrames.

0

Looking at the source you can use None for your frame to go to the parent head frame.

driver.switch_to().frame(None)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.