Select Element By Text in Selenium

后端 未结 4 927
眼角桃花
眼角桃花 2021-01-23 02:17

This may sound so simple but why there is no method to find element by its inner text without using xpath? for instance there is an element:

4条回答
  •  感动是毒
    2021-01-23 03:01

    There is a linkText method:

    driver.findElement(By.linkText("Some Text"))
    

    You also have partialLinkText:

    driver.findElement(By.partialLinkText("Partial Text"))
    

    For examples, see how-to-locate-element-by-link-text-and-partial-link-text-locator

    Edit: As @Cajova_Houba mentioned that it works only for anchor elements

提交回复
热议问题