3

I'm trying to use find_element_by_class_name where the class has spaces and it does not work:

Here is the code:

<a class="yt-simple-endpoint style-scope yt-formatted-string" href="/user/santanderbrasil">Santander Brasil</a>

I want the get the content "Santander Brasil"

I tried the following:

driver.find_element_by_class_name("yt-simple-endpoint.style-scope.yt-formatted-string")    
driver.find_element_by_class_name("a.yt-simple-endpoint.style-scope.yt-formatted-string")

and

driver.find_element_by_class_name("a.yt-simple-endpoint.")

none of the worked...

Any help?

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • 1
    Possible duplicate of [Selenium Compound class names not permitted](https://stackoverflow.com/questions/37771604/selenium-compound-class-names-not-permitted) – JeffC May 09 '18 at 12:52

4 Answers4

3

Use the css selector function instead.

driver.find_element_by_css_selector("a.yt-simple-endpoint.style-scope.yt-formatted-string")
Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • This printed something. But not what I wanted tho. I forgot to mention that The content I want to scrap comes from an AJAX. I want to get the content from the first banner that shows on the home of youtube. – Fernão de Rocha Guerra May 10 '18 at 21:55
  • This is the code that I can see when I inspect Elemen at the content I want to scrap on www.youtube.com: If you try on your yuotube it will be a different content because it´s a local AD.
    Tudo no app, tudo na mão!
    – Fernão de Rocha Guerra May 11 '18 at 19:19
3

Those are three separated classes, find_element_by_class_name() receives only one class as parameter. For example

driver.find_element_by_class_name('yt-simple-endpoint')

The . you added between the classes represent class in css_selector. You can use it to locate the element by all three classes use css_selector

driver.find_element_by_css_selector('.yt-simple-endpoint.style-scope.yt-formatted-string')

Or by xpath

driver.find_element_by_xpath('//a[@class="yt-simple-endpoint style-scope yt-formatted-string"]')
Guy
  • 46,488
  • 10
  • 44
  • 88
0

Try this

driver.find_element_by_xpath("//div[contains(@class, 'yt-simple-endpoint') and contains(@class, 'style-scope') and contains(@class, 'yt-formatted-string')]"")

Not sure about the find_element_by_xpath method because i don't use selenium in python, but whatever that function is, selector should do the trick.

Hope this helps.

Pierre Baran
  • 160
  • 10
0

All of these work for me (Firefox driver):

yt-simple-endpoint
style-scope
yt-formatted-string
yt-simple-endpoint.style-scope.yt-formatted-string

Note that the last class name is actually the same as the first one in your question. It works because Selenium internally converts the class name into a CSS selector and then uses it for the search. If you want to nail things down for specific tags, e.g. only match <a> tags with those classes then you will need to look at CSS selectors and other options such as XPath.

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("file:///tmp/test.html")

for class_name in 'yt-simple-endpoint', 'yt-formatted-string', 'style-scope', 'yt-simple-endpoint.style-scope.yt-formatted-string':
    e = driver.find_element_by_class_name(class_name)
    print(e.text)
driver.close()

Output

Santander Brasil
Santander Brasil
Santander Brasil
Santander Brasil

The test.html file contains:

<html>
<head><title>Test</title></head>
<body>
<a class="yt-simple-endpoint style-scope yt-formatted-string" href="/user/santanderbrasil">Santander Brasil</a>
</body>
</html>
mhawke
  • 84,695
  • 9
  • 117
  • 138