question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

By.classname works differently in Java and Python

See original GitHub issue

🐛 Bug Report

in Python multiple classes can be found by replacing space with ‘.’

To Reproduce Python:

from selenium import webdriver

import time

from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://stackoverflow.com/questions/65579491/find-element-by-class-name-in-selenium-giving-error/65579606?noredirect=1#comment115946541_65579606")
time.sleep(5)
elem = driver.find_element_by_class_name('overflow-x-auto.ml-auto.-secondary.grid.ai-center.list-reset.h100')

print(elem.get_attribute("outerHTML"))


Java:

    ChromeDriver driver=new ChromeDriver();
driver.get("https://stackoverflow.com/questions/65579491/find-element-by-class-name-in-selenium-giving-error/65579606?noredirect=1#comment115946541_65579606");

    WebElement a = driver.findElementByClassName("overflow-x-auto.ml-auto.-secondary.grid.ai-center.list-reset.h100");
    System.out.print(a.getAttribute("outerHTML"));

Detailed steps to reproduce the behavior:

behavior

In python script works fine

In Java noELement exception is thrown: ( it seems the functions add frontslash to the locator

image

image

##Expected behavior:

Both should work as same

Environment

OS: Browser:

Language Bindings version: Java and Python Selenium Grid version (if applicable):3.141.59

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
SalmonModecommented, Jan 8, 2021

Hey there!

You’re right, both should behave the same. However, it’s not my call to make which way should be the way. In the meantime though, I would suggest using a CSS selector instead (that’s what it uses under the hood anyway).

0reactions
diemolcommented, Jan 13, 2021

Java is escaping the locator, which is expected. If you’d prefer to treat the locator as it is, better use By.cssSelector.

What is important to notice is that when By.className("overflow-x-auto.ml-auto.-secondary.grid.ai-center.list-reset.h100") is used, you are expecting an element that looks lke <div class="overflow-x-auto.ml-auto.-secondary.grid.ai-center.list-reset.h100">, and on the provided link, there is no element with that class.

I understand that the locator worked for you with the Python bindings, but there might be small implementation differences between the language bindings, and seems you bumped into one. The important thing to realize here, is that the locator is more suited as a cssSelector.

If you have more questions or comments, please drop by our Slack/IRC channel where we can continue the conversation. Links for the channels can be seen here https://www.selenium.dev/support/

Thanks to @barancev, @shs96c and @AutomatedTester for helping to debug this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object-Oriented Programming in Python vs Java
In this step-by-step tutorial, you'll learn about the practical differences in Python vs Java for object-oriented programming. By the end, you'll be able...
Read more >
Find by Class name command is not working - Stack Overflow
data=driver.find_elements_by_class_name() accepts only single class name. class="next navigation" defines two classes, next and navigation .
Read more >
Python and Java - Comparisons and Contrasts - Rose-Hulman
-java ClassName arg0 arg1 executes that class with given command-line ... on values of different types at different times during the execution of...
Read more >
Myth about the file name and class name in Java
The myth about the file name and class name should be same only when the class is declared in public. The above program...
Read more >
How to Get Class Name in Python? - FavTutor
Similarly, the same blueprint can be used for different houses to be built. The programming works in the flow where first the class...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found