Java 元素在XPath检查器中找到,但在Selenium中找不到

Java 元素在XPath检查器中找到,但在Selenium中找不到,java,unit-testing,selenium,xpath,Java,Unit Testing,Selenium,Xpath,我有以下XPath: //div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href 当我在XPath检查器(Firefox扩展)中尝试这个XPath时,它一直工作得很好。但当我在Selenium中执行以下操作时: System.out.println(selenium.getAttribute("//div[contains(@id, 'box')]/div/h4/small/a[contains(@hr

我有以下XPath:

//div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href
当我在XPath检查器(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
我要发疯去解决这个问题。有人看到我的代码行中有错误吗?

根据,应该是

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

这个查询字符串不应该是这样的吗(根据JavaDocAPI)


在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");
driver.findElement(By.xpath("//div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href")).getAttribute("The name of the attribute");
在Selenium WebDriver中:代码如下:

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

您应该首先编写xpath 乙二醇


这里您的
WebDriver\u Object\u name
可能非常完美!谢谢。。。我想我喝的咖啡因太多了!Selenium应该知道这是一个xpath定位器,因为它以
/
开头。在Selenium中的最后一个
@href
默认定位器出现之前,我会怀疑
/
您在使用Selenium RC吗?