- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Test if an element is focused using Selenium Webdriver.
We can test if an element is focused with Selenium webdriver. This is determined with the help of the activeElement() method. First of all we need to identify the element with help of any of the locators like id, class, name, xpath or css.
Syntax
driver.switchTo().activeElement();
Let us consider the below edit box and check if it is focused.
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class ElementFocussed{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/index.htm"); // identify element WebElement l=driver.findElement(By.cssSelector(".gsc-input")); //activeElement() to verify element focused if(l.equals(driver.switchTo().activeElement())) System.out.println("Element is focused"); else System.out.println("Element is not focused"); driver.close(); } }
Output
- Related Articles
- Test if element is present using Selenium WebDriver?
- How to verify an attribute is present in an element using Selenium WebDriver?
- How do you click on an element which is hidden using Selenium WebDriver?
- Finding an element in a sub-element in Selenium Webdriver.
- How to scroll to element with Selenium WebDriver using C#?
- Verifying whether an element present or visible in Selenium Webdriver
- How to get an attribute value of an element in Selenium Webdriver?
- How to check if an element is visible with WebDriver?
- How to run Selenium WebDriver test cases in Chrome?
- Find elements using Selenium WebDriver?
- Gmail login fail using Selenium webdriver. Showing element not found for password.
- How to read test data from an excel sheet and use it to test facebook login in Selenium Webdriver?
- Clicking on elements within an SVG using XPath (Selenium WebDriver).
- Check that the element is clickable or not in Selenium WebDriver
- How do I find an element that contains specific text in Selenium Webdriver?

Advertisements