2013-07-16 79 views
0

我想找出资源管理器元素的xpath,当我尝试使用此代码, driver.findElement(By.xpath("Resource Manager xpath")).click(),我出现错误说无法找到元素,任何人都可以帮助我因为这对我来说是非常紧迫的,所以在走出XPath的时候,我需要进一步完成我的任务。无法找到元素与Xpath

<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> 
+0

请问您能提一下您使用的实际xpath? – TDHM

+0

糟糕...我没有看到答案。他们一定帮了你。 – TDHM

回答

3

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

这应该工作:

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

感谢您的快速回复,当我使用的代码我没有得到任何错误,但元素没有得到点击.... :( –

+0

@ShivaChalla,那么这是一个不同的问题。 – Arran

+0

@Arran:方案是我想单击资源管理器元素,其中所有元素都是用Java脚本注入的,而页面加载的所有元素都是我在上面提到的代码中提到的。为此,当我将findElement与xpath一起使用时,它无法找到元素请让我知道如果你不清楚... –

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(); 
+0

更好的等待例子是等待该特定元素点击 – Alp

+0

@Alp如果您发现wait.until(),我会这样做。它等待元素准备点击并点击它。 – nilesh

+1

是的你是对的,对不起。优秀的解决 – Alp

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. 
相关问题