Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test which element has the focus in Selenium RC?

How do I test which element has the focus in Selenium RC?

like image 283
david-shang Avatar asked Dec 28 '22 12:12

david-shang


2 Answers

The following locator should select the active element in the document:

dom=document.activeElement

If a form field or similar element has focus, then it should be the active element. Hope this helps.

like image 162
AlistairH Avatar answered Dec 31 '22 01:12

AlistairH


As AlistairH mentions, you can use document.activeElement on most current browsers. To use this in Selenium you can store the active element and compare it to the active element. Below is an example for Selenium IDE, which should point you in the right direction for Selenium RC too.

storeEval | this.browserbot.findElement("name=targetElement").id; | targetElement
storeEval | this.browserbot.getUserWindow().document.activeElement.id; | activeElement
verifyEval | '${targetElement}' | ${activeElement}

Note that the above relies on the element having a unique id assigned.

like image 25
Dave Hunt Avatar answered Dec 31 '22 01:12

Dave Hunt