Cannot get element name by class if class name is a number

I tried to find the element by class name with the following code:

result.find_element_by_class_name("0") 

result is a WebElement containing the element I'm looking for. (I'm sure of it)

html-code :

 <tr class="0"></tr> 

But if I do this, I get the following error:

selenium.common.exceptions.InvalidSelectorException: Message: This selector 0 is either invalid or does not use WebElement. The following error has occurred:

InvalidSelectorError: Invalid or illegal class name specified

There are other WebElements inside result that I can find through the class name (for example, there is an element named last , and if I try to get it, it works)

So, how can I find an element by class name if the class name is Number ? (which is a string)

+4
source share
1 answer

find_element_by_class_name() uses the CSS selector under the hood ( source ):

 if by == By.ID: by = By.CSS_SELECTOR value = '[id="%s"]' % value elif by == By.TAG_NAME: by = By.CSS_SELECTOR elif by == By.CLASS_NAME: # < HERE by = By.CSS_SELECTOR value = ".%s" % value elif by == By.NAME: by = By.CSS_SELECTOR value = '[name="%s"]' % value 

And the problem is that according to the CSS grammar "0" is not a valid class name, since the name must begin with an underscore ( _ ), a hyphen ( - ), or a letter ( corresponding subject ). You can find the XPath element in this case:

 result.find_element_by_xpath("//tr[@class='0']") 
+8
source

All Articles