Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium python find_element_by_class_name() stopped working from v 2.2 to 2.21 -- cannot use 'Compound Class Name'

I am using Selenium's python library to scrape data from a html page in Firefox.

I have had to update from Selenium 2.0 to 2.21 because the server has updated Firefox.

In v 2.21 calls to find_element_by_class_name("grid-cell-inner grid-col-name") fails with:

selenium.common.exceptions.WebDriverException: Message: u'Compound class names not permitted'

The class name of the element I am trying to access is grid-cell-inner grid-col-name

The call to find_element_by_class_name() worked in v 2.2, so the logic is correct, and the data used to be found OK. Something changed in v 2.21.

All the Selenium examples give simple examples with class name foo etc, and none with the type of name I need to access.

Why did Selenium stop supporting finding classes with names like grid-cell inner grid-col-name, and what it their solution?

Can someone please help me to find elements with "compound" class names?

like image 606
Robert Hector Avatar asked May 18 '12 19:05

Robert Hector


2 Answers

The problem about WebDriver is that it still evolves. A lot. I personally don't know about a version that supported searching by many classes in one command, so it must have been a fairly old one :).

Searching by a CSS selector should work, however:

find_element_by_css_selector(".grid-cell-inner.grid-col-name");

I don't recommend using XPath for this particular thing, because these two following expressions are a different thing:

//*[class='grid-cell-inner grid-col-name']

//*[class='grid-col-name grid-cell-inner']

like image 88
Petr Janeček Avatar answered Oct 24 '22 08:10

Petr Janeček


You need to use a CssSelector in the format ".nameA.nameB.nameC" you can have as many as you'd like, just add the "."

Alternatively you can match the whole attribute (you can also do this with xpath): "[class='exact class name here']" XPath - "//[@class='exact class name here']"

There are ways to do starts with or ends with or contains too (in both CSS and xpath) which helps if the classes are generated dynamically.

like image 32
some_other_guy Avatar answered Oct 24 '22 10:10

some_other_guy