【问题标题】:Selenium (Java) message "Unable to locate element" but element and xpath existSelenium (Java) 消息“无法定位元素”但元素和 xpath 存在
【发布时间】:2017-07-04 11:53:35
【问题描述】:

我是 Selenium WebDriver 的新手,使用 NetBeans java 和 Firebug for Firefox 来获取 xpath。 问题是即使某些元素在浏览器中可见且可单击,并且 firebug 找到 xpath,我仍然收到错误“无法找到元素”。问题是,元素不在框架中(没有框架),不在另一个窗口中,它是可见和可点击的,并且它具有清晰的 xpath。 我使用了 Wait 和 Thread.sleep,没有帮助。 此外,尝试了我可以在 Stackoverflow 上找到的所有与此类似的解决方案。 元素在表里面,里面有一些“div”元素。这是 xpath:

.//*[@id='NavigationRadPanelBar_i2_i0_trvStandardView']/ul/li[3]/div/span[3]

面板似乎有问题,因为它也无法定位面板。我在同一个面板中尝试了另一个元素,它的标题和文本是这样的:

driver.findElement(By.xpath("//*[contains(text(),'Technical Attributes')]")).click();   

但仍然收到相同的消息。此面板和面板本身内的所有元素都类似。似乎无论我尝试了什么,都无法找到。此面板之外的所有内容都可以与驱动程序一起正常工作。 页面有一个左侧面板,这些元素所在的位置,有些是可以展开的节点,有些只是链接,但什么都找不到。 什么原因导致元素明显存在且可见,但 WebDriver 仍然无法定位?

我试图找到主面板:

    driver.findElement(By.xpath(".//*[@id='NavigationRadPane']")).click();

但还是一样的例外。

这是html的一部分(很长):

<div id="RAD_SLIDING_PANE_CONTENT_navigatiionRadSlidingPane" class="rspSlideContent" style="overflow: hidden; width: 200px; height: 579px;">
  <div id="NavigationRadPanelBarPanel" style="display: block;">
    <div id="NavigationRadPanelBar" class="RadPanelBar RadPanelBar_Office2007" postback="false" style="background-color:GhostWhite;height:100%;width:100%;">
      <ul class="rpRootGroup">
        <li class="rpItem rpFirst">
          <li class="rpItem">
            <li class="rpItem rpLast">
              <a class="rpLink rpExpandable rpExpanded" href="#">
                <div class="rpSlide" style="display:block;">
                  <ul class="rpGroup rpLevel1 111460" style="display: block; height: 274.731px; width: 100%;">
                    <li class="rpItem rpFirst rpLast">
                      <div class="rpTemplate">
                        <div id="NavigationRadPanelBar_i2_i0_trvStandardView" class="RadTreeView RadTreeView_Office2007">
                          <ul class="rtUL rtLines">
                            <li class="rtLI rtFirst">
                              <li class="rtLI">
                                <li class="rtLI">
                                  <div class="rtMid">
                                    <span class="rtSp" />
                                    <span class="rtPlus rtPlusHover" />
                                    <img class="rtImg" src="..." alt="Technical Attributes" />
                                    <span class="rtIn" title="Technical Attributes">Technical Attributes</span>
                                  </div>
                                  <ul class="rtUL" style="display:none;">
                                </li>
                                <li class="rtLI rtLast">

【问题讨论】:

  • 感谢您提供此信息。如果您可以共享左侧面板元素的 HTML,那就太好了。
  • 我粘贴了部分 html。希望对您有所帮助。
  • 你确定页面第一次加载时它就存在吗?如果它是面板的一部分,则必须单击/展开某些内容才能创建该元素。
  • 有可能。面板有一个“取消停靠”选项(引脚),但引脚当前已放置,因此它会立即打开。即使面板被固定以便打开,这可能是原因吗?
  • 是不是整个面板都被固定了,取消固定后需要点击展开?

标签: java selenium exception xpath webdriver


【解决方案1】:
driver.switchTo().activeElement();

这样驱动程序将切换到您单击的面板。然后您可以在面板中执行其余您想要的操作。

【讨论】:

  • 问题是该面板也“无法定位”。我不知道如何使用它,我试过了,但它仍然抛出异常。
【解决方案2】:

这是您问题的答案:

错误说明了一切Unable to locate element,因为xpath 对我来说似乎不正确。

如果您想点击 title 设置为 Technical Attributes 的元素,您可以考虑尝试使用以下 xpath:

//div[@id='NavigationRadPanelBar_i2_i0_trvStandardView']/ul/li/li/li/div/span[@class='rtIn']

//div[@id='NavigationRadPanelBar_i2_i0_trvStandardView']/ul/li/li/li/div/span[@title='Technical Attributes']

如果这能回答您的问题,请告诉我。

【讨论】:

  • 对不起,刚试了,没用。同样的错误。这条路径似乎是正确的,所以它必须是其他原因,而不是路径。感谢您提供帮助。
  • @BiljanaM。您现在看到的错误是什么?请问可以更新问题区域中的错误堆栈跟踪吗?
  • 相同:线程“main”中的异常 org.openqa.selenium.NoSuchElementException:无法定位元素://div[@id='NavigationRadPanelBar_i2_i0_trvStandardView']/ul/li/li/li/ div/span[@title='技术属性']
  • @BiljanaM。当您在我建议的 xpath 上获得 NoSuchElementException 并且还使用 xpath 作为 .//*[@id='NavigationRadPane'] 时,我确信该元素在 iFrame 内,或者您需要诱导 ExplicitWait。谢谢
  • @BiljanaM。如果 URL 是公共 URL,请考虑共享它,以便我为您提供帮助。谢谢
猜你喜欢
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-08
  • 2020-07-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多