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

时间:2009-11-10 20:36:07

标签: java unit-testing selenium xpath

我有以下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

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

4 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:2)

根据API doc,它应该是

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

答案 2 :(得分:1)

在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");

答案 3 :(得分:1)

您应首先编写xpath 例如

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

此处WebDriver_Object_name可能是硒