Best way to detect element on webpage for seleniumRC in java

What is the best way to detect an element on a web page for automated testing using SeleniumRC using Java ?. I know there are XPath elements and CSS elements, but which one is better?

thanks! Nitin

0
source share
5 answers

In my opinion, XPath is the most accurate way, since you can use XPath to describe the exact position of an element in the DOM, however there are some examples where CSS locators work better than XPaths.

Using the selenium identifier locator is the easiest, but if the item you are looking for is not always useful.

The biggest minus when using XPath is the performance with Selenium RC and browsers with poor JavaScript rendering mechanisms (Think IE6 / IE7). A little work is enough to try to fix this, so if you are using a well-formed XPath that only searches for specific parts of the DOM, this problem should be minimal. Selenium webdriver implementation is much faster.

The resume does have a single answer, it depends on what you want from the locator and which browsers you are testing. I personally use XPath in 99% of cases.

I personally use XPath almost exclusively with a random CSS locator.

+2
source

X-Paths are really accurate. However, they are also very fragile. Any change on the website that moves things can break your tests. Identifiers are by far the least fragile since it doesn't matter if your changes in a, your tests will always find it.

+3
source

CSS selectors are faster but not always available; You can always rely on XPath, but it could hurt performance.

+1
source

Xpath is how I used. This will help if your sites have a unique identifier.

+1
source

The best and fastest way to find an element by id. In many browsers, the time the element spent on it id is linear or even constant. very fast.

For example, for an element defined as:

<input type="text" name="passwd" id="passwd-id" /> 

You should find it as follows:

 selenium.type("passwd-id", "test"); 

Or if you are using the webdriver API:

 element = driver.findElement(By.id("passwd-id")); 

If the elements on your pages do not have identifiers, add them! or ask developers to add them!

The next best way to search for items is by name. This is pretty fast, especially if the name is unique on the page.

For instance:

 selenium.type("passwd", "test"); 

Or if you are using the webdriver API:

 element = driver.findElement(By.name("passwd")); 

A third way to find an element is to use a CSS selector. Modern browsers are very good at finding elements this way.

The worst way to find an element is to use xpath. xpath is slow, fragile, and hard to read.

Simon Stewart, a major contributor to Selenium, answers this question here: http://www.youtube.com/watch?v=oX-0Mt5zju0&feature=related look for a timestamp 39:00

There is also good information here: http://seleniumhq.org/docs/02_selenium_ide.html#locating-elements

+1
source

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


All Articles