0

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 class="icon-standard-container icon-size-standard icon-toolbar-container icon-size-toolbar" 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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
DVC
  • 1

3 Answers3

0

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.

Maire
  • 11
  • 2
  • I am not able to get any element. even simple text box. I am getting NoSuchElementException. – DVC Mar 08 '22 at 13:34
  • Could it be the element that you are using, is it getting returned results when searching in the DOM? I could be wrong but in past I have had similar elements and when trying to interact I get ElementNotInteractable error instead of NoSuchElement which may suggest it could be the element or xpath itself? It could also be a waiting issue that may require a wait for element to be present on the page. @DVC – Maire Mar 08 '22 at 15:20
0

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Can you try to switch to frame using below code which is having frame name instead of xpath:

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

And then try to click on desired element using:

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

You can switch back to your main window with below code:

driver.switchTo().defaultContent();
M. Bilal Asif
  • 675
  • 7
  • 19