Finding WebDriver element with Class Name in java

0 votes

I have to locate the element using Class Name, but the problem is there is a sapce between two words in class name. I don't know how to handle it in Java and neither do i have access to change the class name. Below is the HTML code:

<span>
<a class="current time" href="http://someurl/"   onclick="s_objectID="http://someur/">url</a>
</span> 

Running the below code throws me an error. 

driver.findElement(By.className("current time")).click(); 

Even by locating through 'cssSelector' from Firefox's inspect option did not work for me. what could be wrong? 
PS: This question got good response for jquery. But I was working on Java, can someone help me with this please.

Apr 10, 2018 in Selenium by code_ninja
• 5,920 points
144 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

The better way to handle this element is by locating through cssSelector and not class name because of the spacing inbetween. And i'm assuming that your tag name is 'a' based on your HTML from above. So based on this assumption, the correct usage for locating the element is:

WebElement element = driver.findElement(By.cssSelector("a[class='current time']"));
element.click();

Other options for locating it could be:

WebElement element = driver.findElement(By.cssSelector("a[class='current time']"));
element.click();

Or

WebElement element = driver.findElement(By.xpath("//a[@class='current time']"));
element.click();
answered Apr 10, 2018 by nsv999
• 5,050 points

Related Questions In Selenium

0 votes
1 answer

XPath for the elements with no ID or Name in Selenium Webdriver

Use like this or similar to this: //div[@id="top-level-menu-item-3"]/div[@class="filter-label"] //div[@id="top-level-menu1"] ...READ MORE

answered Apr 17, 2018 in Selenium by Meci Matt
• 9,400 points
861 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,050 points
1,420 views
0 votes
2 answers

Unable to Click on an Element in Selenium (Python) even after finding it.

Here, I give you working script which ...READ MORE

answered Sep 19, 2018 in Selenium by Priyaj
• 56,140 points
5,179 views
0 votes
1 answer

How to perform mouse hovering in Selenium WebDriver (Java)?

You many not actually be able to ...READ MORE

answered Apr 3, 2018 in Selenium by nsv999
• 5,050 points

edited Apr 4, 2018 by nsv999 2,103 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
536 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
134 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

Hey Hemant, for installing Selenium Webdriver with ...READ MORE

answered May 8 in Selenium by Anvi
• 7,800 points
26 views
0 votes
1 answer

Finding parent element in Protractor

To get the parent element, you can ...READ MORE

answered Mar 26, 2018 in Selenium by nsv999
• 5,050 points
699 views
0 votes
1 answer

Finding an element in Selenium

Basically, they are not too different. The ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,050 points
74 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.