【问题标题】:Xpath unable to Locate Nested Frame ElementsXpath 无法定位嵌套的框架元素
【发布时间】:2013-04-16 13:13:07
【问题描述】:

使用 Xpath 查找嵌套框架的元素时出现问题。我的代码是:

<html>
<head>
<frameset border="0" framespacing="0" frameborder="0">
<frame noresize="" name="main" src="main.py">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<body>
<div id="cboxOverlay" style="display: none;"></div>
<div id="colorbox" class="" style="padding-bottom: 57px; padding-right: 28px; display: none;">
<div id="main_loading" class="main_loading"></div>
<div id="spin_loading" class="spin-loading"></div>
<div id="page_header">
<div id="header2">
<div id="daemons_controller.py" class="header2_menu_div" style="display:none;"></div>
<div id="main_report.py" class="header2_menu_div" style="display:none;"></div>
<div id="manage_host.py" class="header2_menu_div" style="display:none;"></div>
<div id="main.py" class="header2_menu_div"></div>
<div id="header2_div" style="display:none;">
**

    <div id="login_user">
    <img title="User" alt="" src="images/new_icons/user.png" style="width:16px;height:16px;">

**
<span>
</div>
</div>
<div id="user_options">
<div id="ubr_sub_menu" class="sub-sub-menu" style="right:auto;">
<div id="ubre_sub_menu" class="sub-sub-menu" style="right:auto;">
<div id="idu_sub_menu" class="sub-sub-menu" style="right:auto;">
<div id="header3">
<div id="container">
<div id="events_logs_box" style="display: none;">
<div id="footer">
<div style="clear:both;"></div>
</body>
</html>
</frame>
</frameset>
<pixelperfectlastoverlay id="ppReloadLastOverlay"></pixelperfectlastoverlay>
</html>

Xpath=//div[@id='login_user'] 结果给出了一个空结果。 我需要该 id 的位置,以便我的 selenium 工具找到元素。有什么方法可以通过 Xpath Locator 定位框架。

【问题讨论】:

  • $driver-&gt;switchTo()-&gt;frame($driver-&gt;findElement(WebDriverBy::name('main')))

标签: html xpath selenium-webdriver


【解决方案1】:

使用此方法查找 iframe 内的元素:

driver.switchTo().frame((WebElement) driver.findElement(By.xpath("//iframe")));

现在您在 iframe 中,您可以以正常方式找到任何元素,因为您已经在 iframe 中。如果要切换到父窗口,请使用:

driver.switchTo().defaultContent();

【讨论】:

    【解决方案2】:

    试试这个 xpath,问题可能是因为你使用的是 () 而不是 []

    //div[@id='login_user'] 
    

    【讨论】:

    • 由于嵌套框架而出现的问题不起作用。问题是由于嵌套框架引起的。有任何方法可以找到嵌套框架中元素的位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2021-02-11
    • 2016-07-01
    • 1970-01-01
    • 2021-04-07
    • 1970-01-01
    相关资源
    最近更新 更多