11

I'm writing a Python program that uses Selenium to navigate to and enter information into search boxes on an advanced search page. This website uses Javascript, and the IDs and Names for each search box change slightly 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, for example, has the class name x-form-text x-form-field x-form-num-field x-form-empty-field, but I can't use this because 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, if that can't be done, find a way to search for all elements whose class name contains a section of text without spaces, such as x-form-text.

Any help or thoughts would be greatly appreciated!

Edit:

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.

Andrew Kerrigan
  • 131
  • 1
  • 2
  • 8
  • 2
    Possible duplicate of [Compound class names not permitted error Webdriver](https://stackoverflow.com/questions/32043877/compound-class-names-not-permitted-error-webdriver) – Grasshopper Dec 05 '17 at 18:45

4 Answers4

20

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

Prakash Dahal
  • 4,388
  • 2
  • 11
  • 25
Dalvenjia
  • 1,953
  • 1
  • 12
  • 16
3

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.

Evya
  • 2,325
  • 3
  • 11
  • 22
  • I tried this, but I get an error that says 'List Index Out of Range' implying that it failed to find anything with that css selector. The code I tried is:`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)` @Dalvenjia – Andrew Kerrigan Dec 05 '17 at 19:29
  • Can you post the url you’re trying this on ? – Evya Dec 05 '17 at 19:39
  • Unfortunately the URL is behind a login page that only specific people have access to. Is there other information I could give you that might serve as a placeholder for the URL? – Andrew Kerrigan Dec 05 '17 at 19:42
  • 1
    Another `"."` is required at the beginning of the css selector i.e. `quantminclass = '.x-form-text.x-form-field.x-form-num-field.x-form-empty-fiel‌​d'` – Amit Dec 05 '17 at 19:51
  • Oh my.. Amit is right of course. @AndrewKerrigan Try his version and let us know. – Evya Dec 05 '17 at 19:52
  • That worked perfectly. Thank you! I missed the first '.' in @Dalvenjia 's answer. – Andrew Kerrigan Dec 05 '17 at 19:56
2

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']")
weAreStarsDust
  • 2,634
  • 3
  • 10
  • 22
0

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']")
Jackssn
  • 1,346
  • 1
  • 14
  • 16