Checking an element has focus using Selenium-Webdriver and Capybara

11 Jul 2011 | By Matt Robbins | Tags testing capybara webdriver selenium cucumber

Have you ever wanted to check which element currently has focus using Capybara and/or Selenium-Webdriver? The API is not totally obvious so I thought I would put up one option here.

Using vanilla Webdriver:

focused_element = driver.switch_to.active_element 
#now check for something on the found element

Using Capybara:

focused_element = page.driver.browser.switch_to.active_element
#now check for something on the found element

Note: there is no direct api for this in Capybara as far as I am aware, hence the need to revert to the underlying driver.

blog comments powered by Disqus