0

I have been trying in selenium to click span element which has class named bullet. It's a tree structure where it expands it's children once clicked. I have tried in the following way, but it's not working. Below is the UI Code

<ul id="treelist" class="ltree">
<li class="liClosed">
<span class="bullet"> </span>
<b>Setup</b>
<ul>
 <li></li>
 <li></li>............
 .....................
</ul>
</li>
</ul>

The error is: The given selector //*ul[@id='treelist']//li[2]//span is either invalid or does not result in a WebElement

The code I am using is:

WebDriver driver = new FirefoxDriver();
 driver.get("http://somewebapp");    
  WebElement userElement = driver.findElement(By.xpath("//*ul[@id='treelist']//li[2]//span"));

I tried many ways of editing the xpath, but couldn't succeed. Can someone please help solve this issue?

rick
  • 4,665
  • 10
  • 27
  • 44

3 Answers3

1

The XPath expression is indeed invalid, remove the * before the ul:

//ul[@id='treelist']//li[2]//span

Here is an another alternative to locate the desired span tag:

//ul[@id='treelist']//span[@class='bullet']

Or, with a CSS selector:

driver.findElement(By.cssSelector("ul#treelist span.bullet"));
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I tried this too, it throws nosuchelement exception. Unable to locate element: {"method":"xpath","selector":"//ul[@id='treelist']//li[2]//span"} – rick Oct 14 '15 at 16:17
  • Thanks for your time, but same error with all of the above answers--> Unable to locate element: {"method":"css selector","selector":"ul#treelist span.bullet"}. Is there something that I am missing? – rick Oct 14 '15 at 16:23
  • @rick quick check - do you have `iframe` element(s) on the page? – alecxe Oct 14 '15 at 16:37
  • yes there are somethings called frameset and frame in the page with in which the entire elements exist. Sorry I am new to this frame, so are those the reason for this issue? – rick Oct 14 '15 at 17:18
0

If you aren't tied to XPath, you can try a simple CSS selector.

WebElement userElement = driver.findElement(By.cssSelector("span.bullet"));

Looks like you need to deal with an element that exists inside of a frame. You can google lots of examples of how to do this but here's one from SO.

Find elements inside forms and iframe using Java and Selenium WebDriver

Community
  • 1
  • 1
JeffC
  • 22,180
  • 5
  • 32
  • 55
  • CSS selectors are so much more reliable than XPATH, specially when you have semantic class names. The only problem with this answer is that it does not address the problem, it's just a workaround. – Ruan Mendes Oct 14 '15 at 18:06
  • @JuanMendes A lot of it depends on how the XPath is crafted. I see a lot of people just get the full blown XPath by right-clicking some element in the dev toolbar and those many times are very brittle. A handcrafted one is much less brittle but I find CSS selectors much less complicated and faster than XPath searches... but that's just me. – JeffC Oct 14 '15 at 18:09
  • Trying to select elements by class name is very hard with XPATH, specially if you want to select an item with class `object` but not `object-anything-else`. Therefore, if you are adding semantic information to the class attribute, it's just a lot easier to get reliable hooks. – Ruan Mendes Oct 14 '15 at 18:38
  • I have many li elements with class bullet, but I want to select only second li from top in the ul which was why I wrote my code as By.xpath("//*ul[@id='treelist']//li[2]//span") , can you suggest how to get to second li? – rick Oct 15 '15 at 04:05
0

Your span.bullet is not within the second li, it's within the first one. Try

//ul[@id='treelist']//li[1]//span
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • I have many li elements with class bullet, but I want to select only second li from top in the ul which was why I wrote my code as By.xpath("//*ul[@id='treelist']//li[2]//span") , can you suggest how to get to second li? – rick Oct 15 '15 at 04:10