What is the most efficient selector to use with findElement()?

前端 未结 4 1420
陌清茗
陌清茗 2020-12-16 05:26

When working with Selenium web testing, there are a few ways to identify WebElements.

In my experience, I have used the following selectors:

  • Cl
4条回答
  •  死守一世寂寞
    2020-12-16 06:12

    I'd prioritise selectors like this:

    • ID - By.id()
    • Name - By.name()
    • CSS Selector - By.cssSelector()
    • XPath - By.xpath()
    • Tag Name - By.tagName()
    • Link Text - By.linkText()

    However, uniq IDs and Names are not always exist. Also you can use CSS Selectors to locate by ID #element_id or by Name [name=element_name] or by ClassName .element_class, so might be good idea to use CSS Selectors instead ID, Name and ClassName. Css is faster than xPath, so it's better to use it where possible. xPath is good for difficult element locators which CSS Selectors can't find. I also wouldn't use Tag Name and Link Text as you can write the same with xPath(For Link Text //a[text()='link_text'], Tag Name //div) or CSS Selectors(For Tag Name div ).

提交回复
热议问题