Monday, March 14, 2011

facebook + Selenium episode: 1

Started following some Selenium tutorials. With the Selenium IDE in Firefox this seemed like a slam dunk. I would start automating some of my daily facebook tasks, neat. Then I got stopped at the front door, the login button... After trying for about an hour and various googling efforts this is what I found.

It looks like  facebook assigns a new identifier to the login button every time the page loads like such:


I tried various methods to select the button, but failed  unttil  I found out about xpath and xPather. Using xPather I got this back in return:

/html[@id='facebook']/body/div[@id='globalContainer']/div[1]/div/div/div/form[@id='login_form']/table/tbody/tr[2]/td[3]/label/input[@id='u466841_3']

That did not work, just a bit too much cruft in there, but reducing the selection to this did:
'login_form']/table/tbody/tr[2]/td[3]/label/

Here is the syntax in the IDE recorder:
xpath=id('login_form')/table/tbody/tr[2]/td[3]//label/



This is the selenium script that ended up working (in its native html format):
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head profile="http://selenium-ide.openqa.org/profiles/test-case"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="selenium.base" href="http://www.facebook.com/" /><title>fb_login</title></head><body><table cellpadding="1" cellspacing="1" border="1"><thead><tr><td rowspan="1" colspan="3">fb_login</td></tr></thead><tbody><tr> <td>open</td> <td>/</td> <td></td></tr><tr> <td>type</td> <td>email</td> <td>your_test_email@gmail.com</td></tr><tr> <td>type</td> <td>pass</td> <td>your_test_pw</td></tr><tr> <td>clickAndWait</td> <td>xpath=id('login_form')/table/tbody/tr[2]/td[3]//label/</td> <td></td></tr>
</tbody></table></body></html>

I hope this helps somebody save an hour, or if you have a better method, please do at it in the comments.