Legend
Legend

Reputation: 116950

Element is found in XPath Checker but not in Selenium

I have the following XPath:

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

When I try out this XPath in XPath Checker (Firefox extension), it works perfectly all the time. But when I do the following in Selenium:

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

It keeps giving me the following log error:

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

I am going crazy to solve this problem. Does anyone see any mistake that I have in my code line?

Upvotes: 3

Views: 9516

Answers (4)

Sagar Ajmire
Sagar Ajmire

Reputation: 343

You should write xpath first eg

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

Here your WebDriver_Object_name might be selenium

Upvotes: 1

Ripon Al Wasim
Ripon Al Wasim

Reputation: 37816

In Selenium RC: it needs to mention xpath as "xpath=//div[contains(@id, 'box')]/div/h4/small/a[contains(@href, 'google')]/@href". So in your case, the code is as below:

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

In Selenium WebDriver: the code is as below:

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

Upvotes: 1

jitter
jitter

Reputation: 54615

Shouldn't that query string look like this (according to javadoc api)?

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

Upvotes: 4

Jim Garrison
Jim Garrison

Reputation: 86774

According to the API doc, it should be

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

Upvotes: 2

Related Questions