Pages

Search This Blog

Tuesday, May 18, 2010

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>

8 comments:

  1. Awesome documentation Niraj .
    really helps anyone who are new to selenium
    Thanks :)

    ReplyDelete
  2. Could you give me your email id , so that i could reach you if required . As Iam beginner , iam facing issues in writing the syntax for locators .I would like to have your guidance at the initial stage
    Could you mail your id to reachreenadey@gmail.com

    ReplyDelete
  3. Heya i am for the primary time here. I found this board and I
    to find It truly helpful & it helped me out much.
    I am hoping to present one thing back and aid others such as you aided me.


    Feel free to surf to my website: filing bankruptcy in florida

    ReplyDelete
  4. Unable to identify the object using "css=input[style='width: 120px; font-size: 12px;"

    Can you pls help me to understand this.

    ReplyDelete
  5. I try to apply same thing for google search box but failed to identify.

    ReplyDelete
  6. Get 90% accurate commodity tips and option trading tips for sure gains. For free trial give us a miss call at 080 3008 8464

    ReplyDelete
  7. 192.168 l 254.254 router login admin password informations. With http://192.168.1.254 ip address, you can perform many operations such as wifi password change, router configuration, Ip configuration, Iptv, port opening via this Ip.

    ReplyDelete