How to choose a frame using selenium?

I use Java to create selenium test cases. My system is based on connected portlets. I use the selectFrame command to select a portlet.

I tried a lot of things, but it seems that it does not work like this:

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

Can anybody help me?

+4
source share
4 answers

You have an XPath expression that should get you the IFrame element that you need. However, you are not talking Selenium about the XPath expression. Below is what you need:

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

Please note: my Java is not better, so this may cause compilation problems, but you should see this idea.

First, find the element by pointing Selenium to the XPath expression you give it, then use that element and paste it directly into the 'switch to frame' expression.

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

We can provide frame id name, id, index, and webelement 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 a web page, we can use "index" . Index values ​​help you easily switch between frames. The index starts with Zero ie if the web page has only one frame, then its index will be zero. If we do not know the number of frames, we can use the findElementBytabname () method

Syntax: -

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

We have a try and catch function if the frame is no longer available. This is a throw exception NoSuchFrameException ()

Use name as a locator to search for a frame Syntax: -

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

Use WebElement to switch frame

Syntax: -

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

I managed to select the "src" frame with no name or identifier using this method and Python Selenium. I found the xpath of the element and made this code to get selenium, to select the frame correctly (using Python 2.7):

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

0
source

Source: https://habr.com/ru/post/1498266/


All Articles