- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
How to find Elements in a Webpage using JavaScript in Selenium?
We can find elements in a web page with the help of JavaScript. We can also validate the elements returned by the JavaScript methods in the browser Console (Pressing F12). JavaScript methods to find elements are −
getElementsByTagname
To obtain a collection of elements with the matching tagname passed as parameter to the method. If there are no matching elements, an empty collection is returned.
Syntax
document.getElementsByTagName("<name of tag>") To get the first matching element, document.getElementsByTagName("<name of tag>")[0]
getElementsByName
To obtain a collection of elements with the matching value of name attribute passed as parameter to the method. If there are no matching elements, an empty collection is returned.
Syntax
document.getElementsByName("<value of name attribute>") To get the first matching element, document.getElementsByName("<value of name attribute >")[0]
getElementsByClassName
To obtain a collection of elements with the matching value of class attribute passed as parameter to the method. If there are no matching elements, an empty collection is returned.
Syntax
document.getElementsByClassName("<value of class attribute>")
To get the first matching element,
document.getElementsByClassName("<value of class attribute >")[0]
getElementId
To obtain an element with the matching value of id attribute passed as parameter to the method. It normally yields a single element since the value of the id attribute is unique in a page.
If there is no matching element, null is returned.
Syntax
document.getElementById("<value of id attribute>")
querySelector
To obtain an element with the matching value of css expression passed as parameter to the method. If there is no matching element, null is returned.
Syntax
document.querySelector("<css expression>")
- Related Articles
- How to scroll down a webpage in selenium using Java?
- How to refresh a webpage using Python Selenium Webdriver?
- How to loop through a menu list on a webpage using Selenium?
- How to get details of a webpage like url, title name, domain name etc using Javascript Executor in Selenium?
- How to perform vertical scrolling of a webpage with Javascript executor in Selenium with python?
- How to redirect to another webpage using JavaScript?
- How to get screenshot of full webpage using Selenium and Java?
- How to get the inner text of a webpage with a Javascript executor in Selenium with python?
- How to send keyboard input to a textbox on a webpage using Python Selenium webdriver?
- Find elements using Selenium WebDriver?
- How to get the title and URL of a webpage with Javascript executor in Selenium with python?
- How can I verify Error Message on a webpage using Selenium Webdriver?
- How to execute a Javascript using Selenium in Python?
- How to create Pay Roll Management Webpage using JavaScript?
- Is it possible to scroll down in a webpage using Selenium Webdriver programmed on Python?
