user2637419
user2637419

Reputation: 25

Unable to locate element with cssSelector

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.

Upvotes: 1

Views: 11667

Answers (3)

Vince Bowdren
Vince Bowdren

Reputation: 9218

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.

Upvotes: 0

Alpha
Alpha

Reputation: 14106

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.

Upvotes: 0

conor
conor

Reputation: 155

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')]")

Upvotes: 4

Related Questions