Selenium: How to find element by partial href?

前端 未结 2 1940
被撕碎了的回忆
被撕碎了的回忆 2021-02-15 16:01

Working code 1:

Driver.Instance.FindElement( By.XPath(\"//a[contains(@href,\'\" + PartialLinkHref + \"\')]\" ));

Working code 2:



        
2条回答
  •  一整个雨季
    2021-02-15 16:38

    The problem with your initial selector is that you're missing the // in front of the selector. the // tells XPath to search the whole html tree.

    This should do the trick:

    Driver.Instance.FindElement(By.XPath("//a[contains(@href, 'long')]"))
    

    If you want to find children of an element, use .// instead, e.g.

    var element = Driver.Instance.FindElement("..some selector..")
    var link = element.FindElement(".//a[contains(@href, 'long')]"))
    

    If you want to find a link that contains text and not by the href attribute, you can use

    Driver.Instance.FindElement(By.XPath("//a[contains(text(), 'long')]"))
    

提交回复
热议问题