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,270 points
3,752 views

2 answers to this question.

+1 vote

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,520 points
0 votes
driver.findElement(By.className("current time")).click();


driver.findElement(By.className("_14fdu48d")).click();


or use 

WebElement element = driver.findElement(By.id("submit"));

// Action can be performed on Input Button element

element.submit()

or 

id , name other locators


answered Sep 3 by Sri
• 2,970 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,460 points
2,923 views
0 votes
1 answer

How to print text from a list of all web elements with same class name in Selenium?

@Rajni, following test case is somewhat similar ...READ MORE

answered Jun 20, 2019 in Selenium by Avantika
20,224 views
0 votes
1 answer
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,736 views
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,890 points
2,726 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,078 views
0 votes
1 answer

Installing Selenium Webdriver with Python package

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

answered May 8, 2019 in Selenium by Anvi
• 14,110 points
3,443 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,520 points
1,998 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 Aug 23, 2019 in Selenium by Abha
• 27,930 points
334 views