Skip to content
Advertisement

find_element(By.CLASS_NAME…) InvalidSelectorException

I need to navigate to the object with special class, that changes every page refresh

So i decided to use bs to find the element class, that works, but selenium raises an exception about invalid selector. class is existing, i can find it in page source. There are some spaces at the beginning and at the ending of class name that bs not showing fo some reason, but even if I add them to the class name manually, problem is still there. `

JavaScript

` page source

Advertisement

Answer

driver.find_element expects two arguments: By and a string containing a class name.
vehicle_menu_class variable is built using this line: vehicle_menu_class = '" ' + str(vehicle_menu_parent)[11:50] + ' "'. So, in this case it is " x-grid-cell x-grid-cell-gridcolumn-1331 ".
The spaces inside the class attribute are class names separators. This element has 2 classes: x-grid-cell and x-grid-cell-gridcolumn-1331. So you need to skip all the spaces.
In the end your locator should be constructed like that:

JavaScript

But what appears to be constructed now is:

JavaScript

It’s hard for python and selenium to understand these quotes and spaces.
I think building the class name like that should work:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement