15

When I go to a certain webpage I am trying to find a certain element and piece of text:

<span class="Bold Orange Large">0</span>

This didn't work: (It gave an error of compound class names...)

elem = browser.find_elements_by_class_name("Bold Orange Large")

So I tried this: (but I'm not sure it worked because I don't really understand the right way to do css selectors in selenium...)

elem = browser.find_elements_by_css_selector("span[class='Bold Orange Large']")

Once I find the span element, I want to find the number that is inside (the content).

num = elem.(what to put here??)

Any help with CSS selectors, class names, and finding element text would be great!

Thanks.

Oh! and my other problem is that there are multiple of those exact span elements but with different numbers inside. How can I deal with that?

Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
Marcus Johnson
  • 2,505
  • 6
  • 22
  • 27

2 Answers2

37

you want: elem.text to get "the number that is inside".

explanation:

in your example, elem is an instance of webdriver's WebElement class (from selenium.webdriver.remote.webelement)

a WebElement instance has several properties, including:

  • tag_name
  • text
  • size
  • location
  • parent
  • id

.text property contains "the content"


"and my other problem is that there are multiple of those exact span elements ..how can I deal with that?"

please don't ask several questions in one.. it doesn't work well with SO's format.

but basically, use the find_elements_* instead of find_element_*. your locator can then return a list of matching WebElement instances instead of a single matching instance... which you can filter further.

Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • 1
    Sorry about the question...great answer though!! Actually, I was wondering why your name looked familiar and then I realized that I was on your website last night when I was looking up python and selenium! There isn't very much out there so yours was on the first page... – Marcus Johnson Aug 18 '12 at 16:27
  • the latest doc for `text` is [WebElement.text](http://selenium-python.readthedocs.io/api.html#selenium.webdriver.remote.webelement.WebElement.text) – crifan Nov 18 '17 at 13:33
0

The problem is actually with the class name as the spaces causes the element to have 3 different classes (separated by the space)

If you set the class to Bold-Orange-Large instead it should work as expected with the elem.text method.

Sam
  • 2,427
  • 2
  • 23
  • 26