元素在XPath Checker中找到,但在Selenium中找不到

我有以下XPath:

//div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href 

当我在XPath Checker(Firefox扩展)中试用这个XPath时,它始终可以正常运行。 但是当我在Selenium中执行以下操作时:

 System.out.println(selenium.getAttribute("//div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href")); 

它一直给我以下日志错误:

 14:30:56.311 INFO - Got result: OK on session 5a1401d374a04779bbe6f7fe9a0b4536 14:30:56.315 INFO - Command request: getAttribute[//div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href, ] on session 5a1401d374a04779bbe6f7fe9a0b4536 14:30:56.372 INFO - Got result: ERROR: Element //div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/ not found on session 5a1401d374a04779bbe6f7fe9a0b4536 

我疯了解决这个问题。 有没有人在我的代码行中看到任何错误?

不应该查询字符串看起来像这样(根据javadoc api)?

 "xpath=//div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href" 

根据API文档 ,它应该是

 ...getAttribute("xpath=//div[contains.... 

在Selenium RC:它需要提到xpath为“xpath = // div [contains(@ id,’box’)] / div / h4 / small / a [contains(@ href,’google’)] / @ href” 。 所以在你的情况下,代码如下:

 selenium.getAttribute("xpath=//div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href"); 

在Selenium WebDriver中:代码如下:

 driver.findElement(By.xpath("//div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href")).getAttribute("The name of the attribute"); 

你应该首先编写xpath例如

 WebDriver_Object_name.findElement(By.xpath("...xpath...")).getAttribute("..Attri_name.."); 

这里你的WebDriver_Object_name可能是selenium