Skip to content
Advertisement

Can find Class Name using Selenium Python

Below is a snippet of the HTML using page_source method in selenium.

Why can’t I find className cell-content cell-colshopCapacity ?

'<div unselectable="on" class="x-grid-cell-inner" style="text-align:left;">Daily</div></td><td class="x-grid-cell x-grid-td x-grid-cell-templatecolumn-1065 x-grid-cell-last x-unselectable "><div unselectable="on" class="x-grid-cell-inner" style="text-align:left;"><span class="cell-content cell-colshopCapacity">101</span></div></td></tr></tbody></table></div></div></div><div class="x-container save x-container-default x-box-layout-ct" style="padding: 5px 15px 18px 9px; width: 907px;"

Using the below returns an empty list []

from selenium import webdriver

driver = webdriver.Chrome(r'path/to/chromedriver')
driver.find_elements_by_class_name('cell-content cell-colshopCapacity')

If I use driver.page_source in vs code it returns the HTML from the webpage which is how I was able to produce the above snippet. Am I correct in assuming if driver.page_source returns HTML with the said class name then I should be able to find it using the below, given that if it was in another frame then it wouldn’t return the class name I am trying to find? Or do I have that assumption wrong?

driver.find_elements_by_class_name('cell-content cell-colshopCapacity')

Advertisement

Answer

class name do not work with spaces in Selenium.

so try changing that to css_selector instead :

driver.find_elements_by_css_selector('span.cell-content.cell-colshopCapacity')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement