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
• 6,220 points
445 views

1 answer to this question.

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,070 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,420 points
1,144 views
0 votes
1 answer
0 votes
1 answer
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,070 points
1,835 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
793 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
435 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
• 12,010 points
115 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,070 points
860 views
0 votes
2 answers

Finding an element in Selenium

Hi, ​both find_element_by_xpath(xpath) and find_element(By.XPath, xpath) serve the ...READ MORE

answered 3 days ago in Selenium by Abha
• 20,280 points
106 views