1

I am new to Selenium-WebDriver. Trying to locate an element and click on it.

But constantly getting below error:

unable to locate an element

Firepath provided following xpath:

"xpath = html/body/header/div/ul/li[2]/a/span[1]"

The relevant HTML code is:

<ul class="nav navbar-nav navbar-right top-nav ">
<li class="identity has-icon">
<a href=".......">
</li>
<li class="settings has-icon">
<a href=".......">
<span class="icon icon-cogs" aria-hidden="true" role="presentation"/>
**<span class="nav-title">Settings</span>**
</a>
</li>

I have tried to find the element in following ways:

1. driver.findElement(By.xpath("//div[@id='right-nav']//a[contains(text(),'Settings')]")).click();

2.  driver.findElement(By.xpath("//div[@id='nav-title']//a[contains(text(),'Settings')]")).click();

3. driver.findElement(By.xpath("//div[contains(@class, 'nav-title')]")).click();

Could someone help me find a solution?

Rao
  • 20,781
  • 11
  • 57
  • 77

6 Answers6

1

You are almost there, In last xpath you have used div, instead of that try using span.

"//span[contains(@class, 'nav-title')]"
Amol Chavan
  • 3,835
  • 1
  • 21
  • 32
0

It does not require a contains there and that too all of your statement contains // twice in xpath which is incorrect.

Please use the below statement

//@ is used to get the span class which is nav-title and 
// whose span text is Settings
driver.findElement(By.xpath("//li/a/span[@class ='nav-title' and . = 'Settings']")).click();
Rao
  • 20,781
  • 11
  • 57
  • 77
  • Thank you. This works. Why does adding "//li/a...." to relative path able to find the element but "//span..." shows "Element currently not visible error"? What does the "." syntax means in the code? – user6507067 Jul 03 '16 at 23:25
  • It is not about just that change alone, it also depends on additional identifying factor with value of the element text using `and`. And would appreciate if you accept it as [answered](http://stackoverflow.com/help/someone-answers) – Rao Jul 04 '16 at 04:46
0

You need to click on "a (link)" tag directly because inner tags are acting as css only (page looks to be using bootstrap). Try following code, it should work

driver.findElement(By.xpath("//a[contains(@href,'PUT href texts here')]")).click()

If this is not working , please mention browser and selenium version in the comment.

Pankaj Kumar
  • 61
  • 2
  • 9
  • @user6507067 I am happy that it worked. Actually you can use this trick to find the xpath of element for the pages which are written in modern web technology frameworks (bootstrap , angular js etc ). – Pankaj Kumar Jul 04 '16 at 09:51
0

You need to click on the span element with text "Settings". so the below xpath will search for a span element which contains Settings as text inside any anchor tag.

.//a/descendant::*[contains(text(),'Settings')]
Sudharsan Selvaraj
  • 4,792
  • 3
  • 14
  • 22
  • Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge. – abarisone Jun 24 '16 at 06:58
  • I received "Unable to locate element" for above solution. – user6507067 Jul 03 '16 at 23:43
0

Try any of the below. you will get the element

//span[@class='icon icon-cogs']
//span[@class='icon icon-cogs'][@role='presentation']
//span[@class='icon icon-cogs'][@role='presentation']/span

Hope it will help you

Andrejs
  • 10,803
  • 4
  • 43
  • 48
Ravi Potnuru
  • 191
  • 4
  • 13
0

This simple CSS selector might work. It's hard to tell if it's unique enough without more of the HTML of the page. It's looking for a SPAN tag with the class nav-title.

driver.findElement(By.cssSelector("span.nav-title")).click();
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • I receive following error: "Element is not currently visible and so may not be interacted with". – user6507067 Jul 03 '16 at 22:13
  • That means you are finding the right element, now you have to make it visible. I can't help you with that without the page HTML. You'll have to find the element on the page manually and figure out how to make it visible... opening a panel, etc. – JeffC Jul 04 '16 at 00:09