osgadallah
osgadallah

Reputation: 145

How to select a frame using selenium?

I'm using Java to create selenium test cases. My system is based on portlets connected to each other. I'm using "selectFrame" command to select the portlet.

I tried many things but it seems it is not working like this:

driver.switchTo().frame("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]");

driver.findElement(By.id("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]"));

Can anyone help me?

Upvotes: 4

Views: 34282

Answers (5)

hid
hid

Reputation: 1

I was able to select an "src" frame with no name or ID using this method & Python Selenium. I found the xpath of the element, and did this code to get selenium to select the frame properly (using Python 2.7):

driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="Detail-innerCT"]/iframe'))

Upvotes: 0

Kostas Tsiligkiris
Kostas Tsiligkiris

Reputation: 57

You can switch to a frame, using its name or id easily:

driver.switchTo().frame("frame_name");

When you have selected a frame to switch to another frame you have to switch to the parent or root first with something like:

driver.switchTo().defaultContent();
driver.switchTo().frame("other_frame_name");

Upvotes: 0

Preeti Sharma
Preeti Sharma

Reputation: 1

We can give frame name, id, index and WebElement locator for identification

Syntax:-

driver.switchTo().frames(); // Switch to window to frame

driver.switchTo().defaultContent();  // Switch to Frame to window

If we know the total number of frames on the web page, then we can use “ index”. Index values help to easily switch between frames. An index will start from Zero i.e. if a web page has only one frame then its index will be Zero. If we don’t know the number of frames we can use “findElementBytabname()” method

Syntax: -

   try
{
   driver.switchTo().frame(indexnumber);
}
  catch(NoSuchFrameException e)
{
  System.out.println(e.getMessage());
}

We have use try and catch if now frame will not available this throw exception NoSuchFrameException()

Use name as locater to find frame Syntax: -

 try
{
  driver.switchTo().frame(“frameName”);
}
  catch(NoSuchFrameException e)
{
  System.out.println(e.getMessage());
}

Use WebElement for switching frame

Syntax: -

  try
{
  WebElement button=driver.findElement(By.xpath(""));
  driver.switchTo().frame(button);
}
  catch (NoSuchFrameException e)
{
  System.out.println(e.getMessage());
}

Upvotes: 2

jgode
jgode

Reputation: 1869

driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]")));

Upvotes: 2

Arran
Arran

Reputation: 25056

You have an XPath expression that is supposed to get you the IFrame element you need. However you are not telling Selenium it's an XPath expression. The below is what you need:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]"));

Note, my Java is not it's best, so this may cause compilation issues but you should see the idea.

Find the element first, by telling Selenium it's an XPath expression you are giving it, then use that element and stick it right in the 'switch to frame' expression.

Upvotes: 8

Related Questions