1

I'm trying to click on a specific part of an element as the site must use some JS magic to detect where in the element I'm clicking. I can't just click on the row, it has to be on the little black triangle.

I need to click on the small triangle

I've tried clicking on the text which is contained in a span:

The text span

I've also tried clicking on the entire background div that contains the triangle as part of the background:

The entire div background

I've used various combinations of clicking on coordinates and moving the cursor relative to the span like so:

WebElement we = driver.findElement(By.xpath("//span[contains(string(),'Remote PC')]")); Actions clickTriangle= new Actions(driver); clickTriangle.moveToElement(we).moveByOffset(-10, -5).click().perform(); 

How should I click on this little triangle?

Edit

More HTML as requested:

More HTML

Here you can see the triangle is assigned as a background to the div:

Background in CSS

10
  • Have you tried doing an offset from the div element? Commented Oct 31, 2013 at 11:13
  • I haven't, but I suppose I could. Let me try. Commented Oct 31, 2013 at 11:13
  • I don't seem to be having much luck with that either. I've tried various pixel offsets to "poke around" the area. Commented Oct 31, 2013 at 11:25
  • Is the click action associated with the span or the div? I guess its the span which is why youre clicking it. Commented Oct 31, 2013 at 11:33
  • I just tried using the span as a reference point to then move slightly left from and click on the triangle. I've also tried using the div, when you move to an element it moves to the top left corner, then moving down and right from there. Not much luck either. Commented Oct 31, 2013 at 11:37

3 Answers 3

3

Can you try this,

WebDriverWait wait = new WebDriverWait(driver, 300); Actions builder = new Actions(driver); WebElement element = wait.until(ExpectedConditions .elementToBeClickable(By.id("node_0_item"))); Action action = builder.moveToElement(element, 24, 0).click().build(); action.perform(); 

From the Java docs here, xOffset - Offset from the top-left corner. A negative value means coordinates right from the element.

We want to move to the left of the div element, so we need to provide positive value. I selected 24px because It seems to me that the div is right of that image by 24px. If above doesn't work, try a few different values for xOffset. Hope that works for you.

Edit# Added WebDriverWait

Sign up to request clarification or add additional context in comments.

7 Comments

I'm getting org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up on the action.perform();
I added WebDriverWait and updated the answer. Can you give that a shot?
It will be tomorrow before I can try it out but I will give it a shot first thing.
This is very close. Previously the click seemed to be doing nothing but now the row is highlighting yellow. This happens when you click somewhere on the background but not on the triangle. Let me try some different coords and see if it's just a case of hitting the right spot.
Cool. I think you just need to play with the xOffset. Try tweaking yOffset too, but not a whole lot since the height of the triangle is minimal
|
0

You need to use JavascriptExecutor for clicking on elements which are not links or buttons. So try something like

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.querySelector(\"a[id$='node_0_item'] span\").click()"); 

2 Comments

While jsexecutor may work, but it is not recommended option as it does not simulate real user action and can cover up possible issue in your application. Use it as a last resort only.
I think it's still better than messing up with offsets, this can be also very error prone
0

Here is the description of the moveToElement method:

/**

  • Moves the mouse to an offset from the top-left corner of the element.
  • The element is scrolled into view and its location is calculated using getBoundingClientRect.
  • @param toElement element to move to.
  • @param xOffset Offset from the top-left corner. A negative value means coordinates left from
  • the element.
  • @param yOffset Offset from the top-left corner. A negative value means coordinates above
  • the element.
  • @return A self reference.

    */

So it seems that if you are trying to click left from the element then you have to provide negative value for xOffset. Maybe the Java docs got changed since nilesh posted his comment.

The only solution that I've found to get past the org.openqa.selenium.StaleElementReferenceException exception is that I put both the searching of the element and the action on it in a try-catch block that I executed repeatedly in a do-while loop until I got no exception any more or until it performed the re-try for a limited no of times to escape the infinity loop possibility. This exception is thrown in case the page changes since you find the element until you perform the action on it.

Hope this helps you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.