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

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

In my experience, I have used the following selectors:

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

Obviously, when only one parameter can be used to search for an element, we should use it, but when you can use several methods (for example: div below), how to determine which method to use? Are there more efficient selectors than others? Are there any more durable ones?

<div class="class" id="id" name="name">Here a div</div>
+4
4

s & gs...

, div , .

WebDriver driver = new FirefoxDriver();
driver.get("file://<Path>/div.html");
long starttime = System.currentTimeMillis();
//driver.findElement(By.className("class"));
//driver.findElement(By.cssSelector("html body div"));
//driver.findElement(By.id("id"));
//driver.findElement(By.name("name"));
//driver.findElement(By.tagName("div"));
//driver.findElement(By.xpath("/html/body/div"));
long stoptime = System.currentTimeMillis();
System.out.println(stoptime-starttime + " milliseconds");
driver.quit();

.

  • CssSelector: (796ms + 430ms + 258ms + 408ms + 694ms)/5 = ~ 517.2ms
  • : (670ms + 453ms + 812ms + 415ms + 474ms)/5 = ~ 564.8ms
  • : (342ms + 901ms + 542ms + 847ms + 393ms)/5 = ~ 605
  • ID: (888ms + 700ms + 431ms + 550ms + 501ms)/5 = ~ 614
  • Xpath: (835ms + 770ms + 415ms + 491ms + 852ms)/5 = ~ 672.6ms
  • TagName: (998ms + 832ms + 1278ms + 227ms + 648ms)/5 = ~ 796.6ms

@JeffC By.cssSelector() , id . , .

WebDriver driver = new FirefoxDriver();
driver.get("file://<Path>/div.html");
long starttime = System.currentTimeMillis();
//driver.findElement(By.cssSelector(".class"));
//driver.findElement(By.className("class"));
//driver.findElement(By.cssSelector("#id"));
//driver.findElement(By.id("id"));
//driver.findElement(By.cssSelector("div"));
//driver.findElement(By.tagName("div"));
long stoptime = System.currentTimeMillis();
System.out.println(stoptime-starttime + " milliseconds");
driver.quit();
  • By.cssSelector(".class"): (327ms + 165ms + 166ms + 282ms + 55ms)/5 = ~ 199ms
  • By.className("class"): (338ms + 801ms + 529ms + 804ms + 281ms)/5 = ~ 550
  • By.cssSelector("#id"): (58ms + 818ms + 261ms + 51ms + 72ms)/5 = ~ 252ms
  • By.id("id") - (820ms + 543ms + 112ms + 434ms + 738ms)/5 = ~ 529ms
  • By.cssSelector("div"): (594ms + 845ms + 455ms + 369ms + 173ms)/5 = ~ 487ms
  • By.tagName("div"): (825ms + 843ms + 715ms + 629ms + 1008ms)/5 = ~ 804ms

- , css , !

+8

, :

  • ID
  • LinkText/partialLinkText
  • CSS
  • XPath

: , , .. CSS. , CSS, , , . ... TABLE TR TD, CSS Selectors. , name id, id.

, , CSS . , Selenium , CSS , .

linkText/partialLinkText , . , , . By.cssSelector("#someId"), , , By.id() Id.

XPath , . - / , CSS. ( ), XPath , , , ... , 99% # 1-3.

. LinkText partialLinkText, , , . HTML, CSS, , . , . CSS XPath ( ) .

... , , , , , .

+4

:

  • ID - By.id()
  • - By.name()
  • CSS - By.cssSelector()
  • XPath - By.xpath()
  • - By.tagName()
  • - By.linkText()

uniq . CSS #element_id [name=element_name] ClassName .element_class, CSS- ID, Name ClassName. Css , xPath, , . xPath , CSS. , xPath ( //a[text()='link_text'], name //div) CSS Selectors ( div).

+2

, . :

  • ID. , , .
  • - , , .
  • CSS - , XPath - . Dave Haeffner .
  • XPath. CSS Axis, . ::parent , contains().

LinkText TagName, , , - .

CSS XPath. //div/li[1]/*/span[2] div > li:nth-child(1) , .

+1
source

Source: https://habr.com/ru/post/1663012/


All Articles