【问题标题】:cannot locate dynamic element after switch back to a frame切换回框架后找不到动态元素
【发布时间】:2018-06-27 17:45:12
【问题描述】:

我正在使用机器人框架和 selenium 库来自动测试网站。

我需要点击某个框架下的元素的菜单,测试如下:

# switch to default content first
  unselect frame
# switch to level one menu's frame which id is head and click menu1a which id is menu1a
  select frame head
  click menu1a
# switch to level two menu's frame which id is menuFrame and click menu2a which id is menu2a
  unselect frame
  select frame menuFrame
  click menu2a
# do some testing
  …
# then I need to do some other testing in another menu
# switch to default content first
  unselect frame
# switch to level one menu's frame which id is head and click menu1b which id is menu1b
  select frame head
  click menu1b
  unselect frame
# switch to level two menu's frame which id is menuFrame and click menu2b which id is menu2b
  select frame menuFrame
  click menu2b
# do some other testing
  …

执行 click menu2b 时,selenium 引发异常: 找不到带有定位器“menu2b”的元素。

当手动单击menu1a和menu1b时,二级菜单(menu2a,menu2b)在一个div内和同一框架下动态变化,名称为menuFrame,如果我在同一个一级菜单下找到不同的二级菜单,它没问题,如下:

unselect frame
select frame head
click menu1a
unselect frame
select frame menuFrame
click menu2a
# do some testing
  …
unselect frame
select frame head
click menu1a
unselect frame
select frame menuFrame
click menu2c
# do some other testing

我还将 selenium 速度设置为 1 秒,这将在执行每个命令后等待 1 秒,仍然引发异常。

更新:

当我按 F12 找到 menu2b 元素时,html 是:

<li id="22" onclick="showStation(1012);"><a class="innerA" target="mainFrame" href="test.jsp">menu2b</a></li>

但是,当我在执行时打印页面源代码时,html是:

<li id="50" onclick="showStation(1012);"><a class="innerA" target="mainFrame" href="test.jsp">menu2b</a></li>

元素id不同...

【问题讨论】:

    标签: selenium robotframework


    【解决方案1】:

    这个问题的原因还不清楚,可能是元素id被javascript修改了。 几个小时后,我找到了解决问题的方法,即通过文本定位元素,因为元素文本始终不变。

    【讨论】:

    • 或者只使用xpath,这也是一种选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2013-07-03
    • 2015-11-11
    相关资源
    最近更新 更多