Friday, 12 July 2013

How to use Css instead of Xpath in selenium


Sometimes selenium gets confused to identify an element on the web page because more than one elements have the same name and ids so to handle this situation we instructs selenium to identify the object using CSS instead of Xpath.


<tr>
<td>open</td>
<td>http://economictimes.indiatimes.com/</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>css=input[style='width: 120px; font-size: 12px;']</td>
<td>Business</td>
</tr>

In selenium RC you can use below code 

selenium.open("http://economictimes.indiatimes.com/");

selenium.type("css=input[style='width: 120px; font-size: 12px;']", "Business");

There is another way to use css instead of Xpath

Try below code for clicking on search button on www.google.com


<tr>
<td>type</td>
<td>q</td>
<td>www.automationtricks.blogspot.com</td>
</tr>
<tr>
<td>click</td>
<td>css=span.ds > span.lsbb > input.lsb</td>
<td></td>
</tr>

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...