4

I want to find an element which is visible by mouse hover and I need to click on it and it then opens a text field. When I give the XPath for the element in fire-path, it detects 6 instances of the element. I need the first element. I'm using Java and when I try to run the script it gives me error about unable to locate the element

enter image description here

  • You could use direct mouse control either via Sikuli or Kantu to trigger a mouse-over effect that then triggers the DOM change. – Bobby231 Feb 24 '17 at 8:38
1

By definition locators should be unique, if your locator returns 6 matches it means it's not unique. If you want to use hover, you can either use Actions class in selenium to achieve this, or use JavaScript Executor to do a hover( Not recommended though) Alternatively you could change your locators to use css selector(Highly recommended) , class names etc

  • I routinely use locators (names, css class) which return a list of elements, and detect the one needed by some other attribute. I found this approach less flaky than XPath. – Peter M. - stands for Monica Feb 24 '17 at 15:16
0

use getattribute function in the webdriver with the Xpath then you will get that specific instance of the element

  Boolean b= driver.findbyelement(by.xpath("")).getattribute('transparent');

Check b is true or false if it true then you are select the correct instance

Or you can use the css as well

driver.findbyelement(by.xpath("")).getattribute('background-color');
0

The xpath is a good idea but if you keep getting an error, try to use a JavascriptExecutor to click on the element.

WebElement element = driver.findElement(By.xpath("(//(i[@title='Rename' and ng-click='editCampaignName($index)'])[1]"));

        //in order to click a non visible element
        JavascriptExecutor js = (JavascriptExecutor)driver;
        js.executeScript("arguments[0].click();", element);
0

Xpath offers a way to select the Nth matching element of your expression by giving:

//expression[n]

Although this is going to select all the Nth children elements of matching nodes to your expression. So, it will return many results in your case also.

But there is a solution. In order to achieve a global selection of the Nth matching element you need to wrap your expression in parantheses:

(//expression)[n]

So, in your case it would be (note the wrapping parantheses):

(//i[@title='Rename' and ng-click='editCampaignName($index)'])[1]

Let us know if it worked for you.

Credits: https://stackoverflow.com/a/18580255/3652270

  • Thanks for the solution you provided but unfortunately I'm unable to use the above technique to find the element. It is giving unable to locate element error – K.K Feb 24 '17 at 18:59
  • @KrishnaKishore I edited my answer. I changed the opening parenthesis to include also //. It was my mistake. Can you try again? – sanastasiadis Feb 24 '17 at 19:17
  • @KrishnaKishore Are you trying to implement above solution after hovering? try isElementVisible() checking before performing any operating to debug this scenario, I am sure you would be able to figure out what's wrong. – Bradz Aug 24 '17 at 5:54
  • This approach is very brittle and should be generally avoided and only used as a last resort. – Martin Spamer Sep 15 '17 at 15:48
0

I think u can use Actions if u are trying to find an element in a tooltip.For example

Actions tooltip = new Actions(driver);
tooltip.moveToElement(driver.findElement(By.xpath("The xpath that u want to move your mouse there")).build.perform();
//After this, U can then try to get the element in that tooltip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.