Monday, 25 April 2016

Selenium WebDriver

Locating UI Elements


By Link Text


Find the link element with matching visible text.


Example of how to find an element that looks like this:


<a href="http://www.google.com/search?q=cheese">cheese</a>>

WebElement cheese = driver.findElement(By.linkText("cheese"));


By Partial Link Text


Find the link element with partial matching visible text.


Example of how to find an element that looks like this:


<a href="http://www.google.com/search?q=cheese">search for cheese</a>>

WebElement cheese = driver.findElement(By.partialLinkText("cheese"));


By CSS


Like the name implies it is a locator strategy by css. Native browser support is used by default, so please refer to w3c css selectors for a list of generally available css selectors. If a browser does not have native support for css queries, then Sizzle is used. IE 6,7 and FF3.0 currently use Sizzle as the css query engine.

Beware that not all browsers were created equal, some css that might work in one version may not work in another.

Example of to find the cheese below:

<div id="food"><span class="dairy">milk</span><span class="dairy aged">cheese</span></div>

WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy.aged"));


By XPATH

At a high level, WebDriver uses a browser’s native XPath capabilities wherever possible. On those browsers that don’t have native XPath support, we have provided our own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various xpath engines.


DriverTag and Attribute NameAttribute ValuesNative XPath Support
HtmlUnit DriverLower-casedAs they appear in the HTMLYes
Internet Explorer DriverLower-casedAs they appear in the HTMLNo
Firefox DriverCase insensitiveAs they appear in the HTMLYes
This is a little abstract, so for the following piece of HTML:

<input type="text" name="example" />
<INPUT type="text" name="other" />

List<WebElement> inputs = driver.findElements(By.xpath("//input"));

The following number of matches will be found


XPath expressionHtmlUnit DriverFirefox DriverInternet Explorer Driver
//input1 (“example”)22
//INPUT020


Sometimes HTML elements do not need attributes to be explicitly declared because they will default to known values. For example, the “input” tag does not require the “type” attribute because it defaults to “text”. The rule of thumb when using xpath in WebDriver is that you should not expect to be able to match against these implicit attributes.

3 comments:

  1. My friend Suggest me this blog and I can say this is the best blog to get the basic knowledge.Thank you so much for this Selenium Training in Chennai blog

    ReplyDelete
  2. Best blog.Got to learn new things.Thanks for this Blog SAP HR Training in Chennai

    ReplyDelete
  3. Great place to learn about the new things and got great knowledgeSAP MM Training in Chennai

    ReplyDelete