Checking an element has focus using Selenium-Webdriver and Capybara
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.