- 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
Find div element by multiple class names in Selenium?
We can find elements by multiple class names. If there is an element having more than one value separated by spaces set for the class attributes, it is called the compound class names.
Let us see the HTML code of such web elements having compound class names −
We shall get an exception if we use both the values - toc and chapters with the class name locator for the above scenario. Instead, the rule is to have only one class attribute value with the class name locator.
Syntax
WebElement l = driver.findElement(By.className("toc")); //Invalid locator value with className locator WebElement l = driver.findElement(By.className("toc chapters"));
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class ClssLocator{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://www.tutorialspoint.com/about/about_careers.htm/"); //identify element with className having compound classes WebElement l = driver.findElement(By.className("toc")); //verify if element is displayed boolean b = l.isDisplayed(); System.out.println(b); driver.quit(); } }
Output
With the css locator, we can use all the values set for the class attributes in a compound class. This is done by combining all them separated by a dot(.) and also adding a dot(.) at the start of the css expression.
Syntax
WebElement l = driver.findElement(By.cssSelector(".toc.chapters"));
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class CssSelectorLocator{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //launch URL driver.get("https://www.tutorialspoint.com/about/about_careers.htm/"); //identify element with css having compound classes WebElement l = driver.findElement(By.cssSelector(".toc.chapters")); //verify if element is displayed boolean b = l.isDisplayed(); System.out.println(b); driver.quit(); } }
Output
With the css and xpath locator, we can include all values of the class attributes, by using class as attribute and then the entire class value in quotes.
Syntax
WebElement l = driver.findElement(By.xpath("//ul[@class='toc chapters']")); WebElement m = driver .findElement(By.cssSelector("ul[class='toc chapters']"));
Example
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class XpathLocator{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); //launch URL driver.get ("https://www.tutorialspoint.com/about/about_careers.htm/"); //identify element with xpath having compound classes WebElement l = driver.findElement(By.cssSelector("ul[class='toc chapters']")); //verify if element is displayed boolean b = l.isDisplayed(); System.out.println(b); driver.quit(); } }
Output
- Related Articles
- Find Element and FindElements by XPath in Selenium
- JavaScript focus a particular element with div class, not div id declaration?
- Find and click element by title Python Selenium.
- How to find an element using the attribute “class name” in Selenium?
- How can Selenium select each div separately that have the same class?
- Find Element and FindElements in Selenium
- How to find a radio button element by value using Selenium?
- Selenium Webdriver Locating Strategies By Class Name
- Is there a way to find an element by attributes in Python Selenium?
- Find next sibling element in Selenium, Python?
- Difference between find Element and find Elements in Selenium
- Get the text from multiple elements with the same class in Selenium for Python?
- How to remove multiple columns from matrix in R by using their names?
- TestNG error:- Cannot find class in classpath using Selenium
- Finding an element by partial id with Selenium in C#.
