I am trying to hover over an element in a menu bar with selenium, but having difficulty locating the element. The element is displayed below :
You are using incorrect syntax of xpath in By.Xpath
and By.LinkText
works only on a
element with text and By.ClassName
looks ok but may be there are more elements with that class name that's why you couldn't get right element, So you should try use below provided xPath with text :-
driver.FindElement(By.XPath("//div[text() = 'TextToFind']"));
Or
driver.FindElement(By.XPath("//div[. = 'TextToFind']"));
Or
driver.FindElement(By.XPath("//*[contains(., 'TextToFind')]"));
Hope it works...:)
Better ignoring the whitespaces around the text with this:
var elm = driver.FindElement(By.XPath("//a[normalize-space() = 'TextToFind']"));
This searches text within an [a] element, you can replace it with any element you are interested in (div, span etc.).