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.

4

3 回答 3

4

您应该尝试此页面上的答案How can I find an element by CSS class with XPath? . 通过仅搜索“marker-cluster-small”,您应该能够做一些接近您的第一个答案的事情。希望它在某种程度上有所帮助。所以它会是 ("//div[contains(@class, 'marker-cluster-small')]")

于 2013-07-31T09:43:13.033 回答
0

在您的 CSS 选择器中,您只是在告诉它marker-cluster-small——但这不起作用,因为它正在寻找一个元素标签,而不是一个类。为了让它寻找一个类,你需要像这样的点符号:

div.marker-cluster-small

或者

.marker-cluster-small

请记住,类上的 CSS 选择器比 xpath 选择器更有效,因为您可以只命名一个类——即使该元素也有其他类。

所以我推荐这个作为理想的定位器解决方案,简洁高效地使用 CSS:

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

PS CSS在 firefox 上使用firepath验证。

于 2013-08-05T10:04:59.690 回答
0

你也可以通过css找到它:

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

但在这种情况下,conor 的答案是最好的,因为类值太长了。

于 2013-08-01T02:57:26.510 回答