Find element with selenium by display text

后端 未结 2 1255
温柔的废话
温柔的废话 2021-02-20 02:40

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 :

相关标签:
2条回答
  • 2021-02-20 03:42

    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...:)

    0 讨论(0)
  • 2021-02-20 03:43

    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.).

    0 讨论(0)
提交回复
热议问题