2

I am trying to automate webpages with selenium(java).

I am working on an automation tool with which an WebElement can be spied and saved. These saved webobjects can be used to perform certain actions later. When spying an WebElement, I will get (x,y) coordinates with which I execute the following script that returns a WebElement:

webObject = (WebElement) driver.executeScript(String.format("return document.elementFromPoint(%s, %s);",new Object[] { x, y }), new Object[0]);

If (x,y) lies on a different frame, this script returns WebElement of that Frame which can be used for switching the driver. I keep on executing the same script until I get the WebElement which is not a Frame. While saving this, I save xPath which is relative to the current Frame and ID's of the Frames that I have switched so far.

Later, when performing some actions I locate the WebElement based on ID's of the Frames and xPath.

So is there a convention/standard followed by the developers to always create ID attribute for frame/iframe? If this is not true what other attributes I can rely on?

333
  • 148
  • 1
  • 10

2 Answers2

1

There is several ways to work with frame/iframe using Selenium WebDriver. You can refer to below stackoverflow question for more understanding about handling of iframe using Selenium WebDriver:

How to identify and switch to the frame in selenium webdriver when frame does not have id

Community
  • 1
  • 1
Abhishek Yadav
  • 459
  • 5
  • 16
1

As per best practices, each and every frame should have the ID and Name attribute specified. But in real time scenario it is observed sometime the ID/Name of the frame is not directly visible in the current HTML DOM.

Switching to frames

We can switch over to frames by 3 ways.

By Frame Name:

Name attribute of iframe through which we can switch to it.

Example:

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

By Frame ID:

ID attribute of iframe through which we can switch to it.

Example:

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

By Index:

Suppose if there are 100 frames in page, we can switch to the iframe by using index.

Example:

driver.switchTo().frame(0);
driver.switchTo().frame(1);

Switching back to the Main Frame:

We can switch back to the main frame by using defaultContent.

Example:

driver.switchTo().defaultContent();

Let me know if this Answers your Question.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I think index depends on how the frames are loaded, so the next time when the same webpage is opened, the index of a certain frame may change. Going by this I cannot save index to switch frames. If suppose a webpage have neither ID nor name attributes, what other attributes can I save or can you suggest how to locate a frame in any scenario. – 333 May 17 '17 at 10:44
  • IMO, loading of frame depends on the code as it is written so untill & unless the code changes the sequence of the frames will be same. If this Answer caters to your Question can you Accept the Answer as a solution to your Question? Thanks – undetected Selenium May 17 '17 at 14:26