5

I tried to find the link on a page to click:

<a id="folder0" class="js-folder icon-wrap icon-wrap_left menu__item__link menu__item__link_act menu__item__link_unread" href="/messages/inbox" rel="history">
    <span class="js-folder-b-unread js-folder-unread menu__item__link__qnt">7</span>
    <i class="js-folder-ico icon icon_left icon_folders icon_inbox_act"></i>
    <span class="menu__item__link__text menu__item__link__text_linear">Input</span>
</a>

Java code:

driver.findElement(By.xpath(".//*[@id='folder0']/span[2]")).click();

But the driver can't locate the element:

 org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//*[@id='folder0']/span[2]"}
James Dunn
  • 8,064
  • 13
  • 53
  • 87
Bilow Yuriy
  • 1,239
  • 3
  • 13
  • 20
  • Are there any other elements in the DOM with `id="folder0"`? Id's are meant to be unique, so your xpath will look for a 2nd span relative to the FIRST `id="folder0"` it finds, and then stop looking. – Dingredient Aug 30 '13 at 16:01
  • @Pat Meeker .This is unique element in the DOM.I understand what do you mean about that. – Bilow Yuriy Aug 31 '13 at 06:31

3 Answers3

9

Thank you for all answers. I found solution to my problem. The last command was command which was linked with IFrame

WebElement editorFrame = driver.findElement(By.cssSelector("#sentmsgcomposeEditor_ifr"));
   driver.switchTo().frame(editorFrame);

   WebElement body1 = driver.findElement(By.tagName("body"));
   body1.sendKeys(Keys.CONTROL + "a"); 

So i was in IFrame actually because of it i couldn't find any element. I performed following command:

 driver.switchTo().defaultContent();

After that it is possible to find locators.

Bilow Yuriy
  • 1,239
  • 3
  • 13
  • 20
3

You should try to locate by id instead of by xpath.

driver.findElement(By.id("folder0")).click();

Two reasons:

  1. Locating by id is usually faster (see here for why).
  2. Since you're trying to test the link, you don't need to click on an inner <span>, just on the link element itself.

If you still want to use xpath, or still want to get the inner <span>, you can use firebug in Firefox to copy the xpath; this will give you the correct xpath and show you if you made a mistake.

Firebug Copying XPath

Community
  • 1
  • 1
James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • Thank you for verbose explanation. 1) I can't find element by Id I had such message: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"folder0"} – Bilow Yuriy Aug 31 '13 at 07:19
  • 2)I use Firebug and Xpath to find locator i need . – Bilow Yuriy Aug 31 '13 at 07:21
2

Try it without the .

driver.findElement(By.xpath("//*[@id='folder0']/span[2]")).click();
Dingredient
  • 2,191
  • 22
  • 47
  • 3
    @BilowYuriy Although I still like my solution better, this answer actually tells you why what you were trying didn't work. – James Dunn Aug 30 '13 at 17:18
  • @Pat Meeker I tried 'driver.findElement(By.xpath("//*[@id='folder0']/span[2]")).click();' and had org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='folder0']/span[2]"} Command duration or timeout: 30.05 seconds – Bilow Yuriy Aug 31 '13 at 07:04