Home > front end >  Not able to access the element inside the frame using Selenium Java
Not able to access the element inside the frame using Selenium Java

Time:03-09

I am using selenium for testing the web pages. Now I am testing a webpage contain frame. The structure us following.

<frameset title="Application Content">
<frame name="main" src="qweMain.jsp?language=ENG" scrolling="no" title="Main Frame">
#document
<html>
<head> </head>
<body>
<div>
<ou-button img="ico-menu" id="menuButton" usage="toolbar" onclick="main.doClose();main.onTopMenuClick('CI_MAINMENU', event);" title="Menu (Ctrl Alt M)" role="button" tabindex="5" aria-label="Menu (Ctrl Alt M)" onkeypress="onButtonKeyPress(event)"><svg  style=""><use xlink:href="ouaf/assets/svgs/ico-menu.svg#icon"></use></svg></ou-button>
</div>
</body>
</html>
</frame>
<noframes>
Browser not supported
</noframes>
</frameset>

I am trying to get the menu button.

driver.findElement(By.xpath("//ou-button[@img='ico-menu']")).click();

But I am getting NoSuchElementException is happening. I search for the solution. So I got some ideas for this. Some developers recommend to switch the frame before access the element. I tried that too.

driver.switchTo().defaultContent();
new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("/html/frameset/frame")));
driver.switchTo().frame(driver.findElement(By.xpath("/html/frameset/frame")));

But still I am getting the same error. Please let some ideas to resolve this issue.

CodePudding user response:

You could try to use a click with some javascript or scroll into view of the element.

var element = _driver.Driver.FindElement(Selector); var jsExecutor = (IJavaScriptExecutor)_driver.Driver; jsExecutor.ExecuteScript("arguments[0].scrollIntoView(true);", element); element.Click();

Or you could try actions to move to element and click

var myElement = _driver.Driver.FindElement(Selector); var a = new Actions(_driver.Driver); a.MoveToElement(myElement).Click().Perform();

I'm not sure how these will handle work with an iframe but hope these might be of some help.

CodePudding user response:

The desired element is within an <iframe> so to interact with the element you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
  • Induce WebDriverWait for the desired elementToBeClickable.
  • You can use either of the following Locator Strategies:
    • Using cssSelector:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame[name='main'][title='Main Frame']")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("ou-button#menuButton[img='ico-menu'][title^='Menu'][aria-label^='Menu']"))).click();
      
    • Using xpath:

      new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[@name='main' and @title='Main Frame']")));
      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//ou-button[@id='menuButton' and @img='ico-menu'][starts-with(@title, 'Menu') and starts-with(@aria-label, 'Menu')]"))).click();
      

Reference

You can find a couple of relevant discussions in:

  • Related