简体   繁体   中英

Not able to access the element inside the frame using Selenium Java

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.

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.

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:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM