1

I am trying to reach this div and to click it later.

<div class="leaflet-marker-icon marker-cluster marker-cluster-small leaflet-clickable leaflet-zoom-animated" style="margin-left: -6px; margin-top: -6px; width: 12px; height: 12px; opacity: 1; transform: translate(702px, 396px); z-index: 396;" title="13 tracks listened in , UA">

Here's what I'm trying to do:

WebElement cluster = driver.findElement(By.cssSelector("marker-cluster-small"));

Here's what I've tried to do:

WebElement cluster = driver.findElement(By.xpath("//div[@class='leaflet-marker-icon marker-cluster marker-cluster-small leaflet-clickable leaflet-zoom-animated']"));

None of the ways work. The first one throws "Unable to locate element" message. No error appears on second one, but when i do:

cluster.click();

nothing happens. What am I doing wrong? Thanks.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
user2637419
  • 25
  • 1
  • 2
  • 8
  • What is the expected action when clicking this element? – Seanny123 Jul 31 '13 at 09:39
  • If none of the direct commands work then use the JSExecutor as a last resort. Refer - http://docs.seleniumhq.org/docs/03_webdriver.jsp#using-javascript. – Akbar Aug 01 '13 at 05:08

3 Answers3

4

You should try the answer on this page How can I find an element by CSS class with XPath?. You should be able to do something close to your first answer by searching only for "marker-cluster-small." Hope it helps in some way. So it would be ("//div[contains(@class, 'marker-cluster-small')]")

Community
  • 1
  • 1
conor
  • 155
  • 11
  • Thank You! I was able to locate an element, but still cannot click it. On click I guess another part of script should work (It appears like some kind of statistics on clicked element appears in another menu). If anyone has time to see it, please visit locate.tunehog.com. – user2637419 Jul 31 '13 at 10:21
  • Try using `cluster.sendKeys(Keys.ENTER);` and see if that works. Also try different browsers. This might help you get to the bottom of it. – conor Jul 31 '13 at 10:38
  • That didn't work. And I keep getting "the element is not attached to the dom" message from time to time. I need this done only for FireFox yet. – user2637419 Jul 31 '13 at 10:52
  • I think I've found your answer http://stackoverflow.com/questions/5709204/random-element-is-no-longer-attached-to-the-dom-staleelementreferenceexception – conor Jul 31 '13 at 11:02
  • Yeah, I'll try to do something about it. Thanks. – user2637419 Jul 31 '13 at 11:09
  • 1
    I tried cssSelector and xpath css search methods in solutions listed above. I had one case where (Firefox only) these did not find the element and I resorted to this method: driver.getPageSource().contains(marker-cluster-small). For my case this was sufficient because there was a single element with a unique value. – davidjmcclelland Oct 29 '13 at 13:32
0

You can also find it by css:

driver.find_element(:css, '.leaflet-marker-icon.marker-cluster.marker-cluster-small.leaflet-clickable.leaflet-zoom-animated')

But in this case, conor's answer is best since class value is too much lengthy.

Alpha
  • 13,320
  • 27
  • 96
  • 163
0

In your CSS selector, you're just telling it marker-cluster-small – but that doesn't work because it is looking for an element tag, not a class. To make it look for a class, you need the dot notation like this:

div.marker-cluster-small

or

.marker-cluster-small

Remember, CSS selectors on classes work more efficiently than xpath selectors do, because you can name just a single class – even if the element has other classes too.

So I recommend this as the ideal locator solution, using CSS concisely and efficiently:

WebElement cluster = driver.findElement(By.cssSelector(".marker-cluster-small"));

P.S. CSS verified using firepath on firefox.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56