Andrew Kerrigan
Andrew Kerrigan

Reputation: 161

Finding elements by class name with spaces

I'm using Selenium to enter information into search boxes on a search page. This website uses JavaScript and the id's and names for each search box change each time the website is loaded, but the class names remain consistent. Class names are frequently reused though, so my goal is to use find_elements_by_class_name(classname) and then index through that list.

One box has the class name x-form-text x-form-field x-form-num-field x-form-empty-field, but Selenium considers it a compound class name and throws an error. If I use just a portion of it such as x-form-text, it can't find the element. My hope is to either find a way to allow the spaces, or search for all elements whose class name contain a section of text without spaces (such as x-form-text).

I tried this code:

quantminclass = 'x-form-text.x-form-field.x-form-num-field.x-form-empty-field'
quantmin = '25'
browser.find_elements_by_css_selector(quantminclass)[0].send_keys(quantmin)

But got an error that the list index was out of range, implying that it can't find anything. I inspected the element and that is definitely its class name, so I'm not sure how to proceed.

Upvotes: 14

Views: 31201

Answers (5)

Abenezer Berhanu
Abenezer Berhanu

Reputation: 1

If you're using js webdriver, you can find two class names on one tag like this:

<li class="class1 class2">test</li>
driver.findElement(By.css(".class1.class2")

If the content or the tag is more than one, use findElements.

Upvotes: 0

Dalvenjia
Dalvenjia

Reputation: 2033

Those are multiple classes, not a single class with spaces, just use all the classes together.

driver.find_element(By.CSS_SELECTOR, '.class1.class2.class3')

In CSS selector a dot . is a class, you can concatenate any number class names

Upvotes: 24

weAreStarsDust
weAreStarsDust

Reputation: 2742

Since Selenium 4 find_element_by_* is depricated, so you need to use find_element() [Selenium-doc]

from selenium.webdriver.common.by import By

# By CLASS_NAME
driver.find_element(By.CLASS_NAME, "x-form-text.x-form-field.x-form-num-field.x-form-empty-field")

# By CSS_SELECTOR
driver.find_element(By.CSS_SELECTOR, ".x-form-text.x-form-field.x-form-num-field.x-form-empty-field")

# By XPATH
driver.find_element(By.XPATH, "//*[@class='x-form-text x-form-field x-form-num-field x-form-empty-field']")

Upvotes: 3

Jackssn
Jackssn

Reputation: 1604

If you have class name (or another attrs) with spaces, for example:

<div class="target with space or maybe another-long-text">Test 123</div>

This will work:

driver.find_element_by_xpath("//div[@class='target with space or maybe another-long-text']")

Upvotes: 0

Evya
Evya

Reputation: 2375

Try converting class name to a CSS selector.
With a CSS selector, a class named x-form-text x-form-field x-form-num-field turns into .x-form-text.x-form-field.x-form-num-field
So basically just replace spaces with dots and you're good to go.

Upvotes: 3

Related Questions