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
You could use direct mouse control either via Sikuli or Kantu to trigger a mouse-over effect that then triggers the DOM change.– Bobby231Feb 24, 2017 at 8:38
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. Feb 24, 2017 at 15:16
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
Xpath offers a way to select the Nth matching element of your expression by giving:
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:
So, in your case it would be (note the wrapping parantheses):
(//i[@title='Rename' and ng-click='editCampaignName($index)'])
Let us know if it worked for you.
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 Feb 24, 2017 at 18:59
@KrishnaKishore I edited my answer. I changed the opening parenthesis to include also //. It was my mistake. Can you try again? Feb 24, 2017 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. Aug 24, 2017 at 5:54
This approach is very brittle and should be generally avoided and only used as a last resort. Sep 15, 2017 at 15:48
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
Find the xpath of the element from browser
- select the element under which this hidden element is expected to appear.
- Right click and select Force state --> ':hover'
This will expose the element. Get its xpath.
On Selenium-webdriver use moveToElement(<parent_element>).perform() and locate the required element using the xpath previously found