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,300 points
13,670 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,500 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 4, 2020 by Sri
• 3,190 points
Finding WebDriver element with Class Name in java | Edureka Community
[url=http://www.g6ga4jd503cha28b93r83t4g5hw81x9ns.org/]ufkkltmzjv[/url]
<a href="http://www.g6ga4jd503cha28b93r83t4g5hw81x9ns.org/">afkkltmzjv</a>
fkkltmzjv http://www.g6ga4jd503cha28b93r83t4g5hw81x9ns.org/
Finding WebDriver element with Class Name in java | Edureka Community
<a href="http://www.gk0wzjun486710r1n01yw9v2u0l9fe58s.org/">avdbcrviyc</a>
vdbcrviyc http://www.gk0wzjun486710r1n01yw9v2u0l9fe58s.org/
[url=http://www.gk0wzjun486710r1n01yw9v2u0l9fe58s.org/]uvdbcrviyc[/url]

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
7,371 views
0 votes
3 answers

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

Try using List <WebElement> to access all similar elements ...READ MORE

answered Dec 16, 2020 in Selenium by Roshni
• 10,480 points
87,418 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, 2020 in Selenium by Sri
• 3,190 points
9,141 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,910 points
7,220 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 3, 2020 in Selenium by Sri
• 3,190 points
7,872 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,150 points
15,836 views
0 votes
1 answer

Finding parent element in Protractor

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

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
3,700 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
• 28,140 points
1,429 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP