无法找到带有Xpath的元素

时间:2013-07-16 13:16:32

标签: java selenium webdriver selenium-webdriver

当我尝试使用此代码时,我正在尝试找出资源管理器元素的xpath, driver.findElement(By.xpath("Resource Manager xpath")).click(),我出现错误,说无法找到元素。

<div class="os-titlebar navbar navbar-inverse">
   <div class="navbar-inner">
      <div class="container">
          <ul class="nav menu-primary os-navbar-icon">
          <ul class="nav context-menu" style="">
           <ul class="nav os-navbar-icon os-titlebar-shortcuts pull-right os_shortcuts">
                 <li class="os-navbar-icon-home selected">
                 <li class="os-navbar-icon-quicklaunch os_quick_start">
                 <li class="os-navbar-icon-resourcemanager">
                  <li class="os-navbar-icon-apptray">
                  <li class="os-navbar-icon-notifications">
                  <li class="os-navbar-icon-minimise-close">
            </ul>
<form class="navbar-search pull-right ui-os-search-form" action="">
<ul class="nav os-navbar-icon os-desktop-navigation" style="left: 407.5px;">
</div>

3 个答案:

答案 0 :(得分:3)

Resource Manager xpath不是有效的xpath表达式。

这应该有效:

driver.findElement(By.xpath("//li[@class='os-navbar-icon-resourcemanager']")).click()

答案 1 :(得分:2)

使用css选择器而不是xpath。也是WebDriverWait

的一个例子
WebDriverWait wait = new WebDriverWait(driver,300/*timeout in seconds*/);
By findBy = By.cssSelector("li.os-navbar-icon-resourcemanager");
wait.until(ExpectedConditions.elementToBeClickable(findBy)).click();

答案 2 :(得分:1)

你可能只会在两种情况下面对“无法找到元素”。

 1.The Element is yet to Load
     - Put some wait here.
 2. You may try to locate the element wrongly .
     - Double Check your Xpath/ID(what ever)
     - Make sure you are in the same frame where the element is present.If not, switch to the frame then.