uzumaki
uzumaki

Reputation: 1973

How do I figure out the correct class name to search for in Selenium?

I'm trying to locate an element by class name. This is an excerpt from the page source (saved to a file using BeautifulSoup):

<div class="flex flex-column"><div...

If I try to find this element like so:

element = browser.find_element_by_class_name("flex flex-column")

It raises an exception:

Message: no such element: Unable to locate element: {"method":"css selector","selector":".flex flex-column"}

But doing

element = browser.find_element_by_class_name("flex-column")

finds the element (there is only one flex-column).

Why does the error message say "selector":".flex flex-column"? Where does that period come from? And how do I know what the correct name of an element is? I'm only using the inspection tool in Firefox to find class names.

Also, how would I search for an element like this one?:

<span class="absolute w-20 h-20 rounded flex justify-center items-center transition marker-wrapper border border-gray-3 group-hover:border-primary-4 bg-gray-6">

Upvotes: 0

Views: 596

Answers (1)

pmorrowtron
pmorrowtron

Reputation: 46

I believe that method only takes a single class name. So it will only take flex or flex-column.

If it's absolutely necessary to use the whole class name, you can use xpath or css selectors to accomplish this.

xpath selector

driver.find_element_by_xpath("//div[@class='flex flex-column']")

css selector

driver.find_element_by_css_selector("div.flex.flex-column")

Upvotes: 3

Related Questions