Unable to click with Selenium in nested frames

2019-08-30 10:21发布

I am trying to navigate on a page with nested frames. The page structure looks like this:

<frameset name="framesetContainer">
  <frame name="WebTopMenu">
  ...
  </frame>
  <frame name="WebContent">
    <frameset name="framesetTopContainer">
      <frameset name="framesetWSTopMenu">
        <frame name="frameTitle">
        ...
        </frame>
        <frame name="frameTopMenu">
        ...
        </frame>
      </frameset>
      <frameset name="framesetLeftMenuContentContainer">
        <frameset name="framesetLeftMenuContainer">
        ...
        </frameset>
        <frame name="frameContent">
        ...
        </frame>
      </frameset>
    </frameset>
  </frame>
</frameset>

The links to navigate reside in the frameTopMenu frame and the content is loaded into frameContent.

I am using the WebDriver API of Selenium (2.35.0). The following code runs fine without any exception, it finds the correct link but somehow the click() call does not have any effect and the content is not loaded into the inner frame.

driver.switchTo().frame("WebContent").switchTo().frame("frameTopMenu");
driver.findElement(By.id("link01")).click();

Do I miss something?

The frame structure cannot be changed...unfortunately.

2条回答
叼着烟拽天下
2楼-- · 2019-08-30 11:04

Try below solution for nested frames.Hope it works

driver.switchTo().frame("WebContent").switchTo().
  frame("framesetTopContainer").switchTo().
  frame("framesetWSTopMenu").switchTo().
  frame("frameTitle");
查看更多
Lonely孤独者°
3楼-- · 2019-08-30 11:11

switch to any frame element , just use driver.switchTo().frame("framename");

Once we switched to one frame, if we need to switch to another frame, we have to switch to the parent frame.For that use

driver.switchTo().parentFrame();

If you use driver.switchTo().defaultContent();, it may not work. so go for driver.switchTo().parentFrame();, it works fine.

查看更多
登录 后发表回答