Can't find element with Selenium/Python

I’m trying to write my first script for Selenium/Python, so no doubt this is a very silly question. I’m trying to find the element that says “Club Pilot Airmanship” in the following HTML:

This is my Python:

quiz_title = driver.find_element_by_class_name(‘css-api-card-header__title’)

It gives the error: File “C:\Users\RowanB\AppData\Roaming\Python\Python38\site-packages\selenium\w ebdriver\remote\”, line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:“css selector”,“selector”:".css-api-card-header __title"} (Session info: chrome=84.0.4147.125)

Why is it not finding the element? What do I have to do to fix this?

Thanks - Rowan

Welcome, rowan.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

As for your question, I cannot see any element within the HTML you have pasted with a class of css-api-card-header__title. Can you point this element out?

I’m sorry, something has removed double underscores from my code. Here is the actual code:

<body class="widget">
  <div id="widget-page">
    <div class="os-widget-wrapper quiz layout-booklet open_sans_he color-palette-opinionstage state-opening" dir="ltr" id="css-api">
      <div class="os-widget-content css-api-widget-body">
        <div class="widget-opening-card" data-widget-opening-card="" style="opacity: 1;">
          <div class="header-area css-api-card-header css-api-card-header--opening">
            <h1 class="css-api-card-header__title">Club Pilot Airmanship</h1>
            <div class="description-text css-api-card-header__description">Fly right!</div>
          <div class="media-area css-api-widget-media image" style=""><img class="cld-responsive css-api-widget-media__image" data-src=",dpr_auto,f_auto,fl_lossy,q_auto:good,w_auto/v1/polls/pcntjhqxa3ehx3jier4s" src=",dpr_1.0,f_auto,fl_lossy,q_auto:good,w_1100/v1/polls/pcntjhqxa3ehx3jier4s"></div>
          <div class="start-engagement-area">
            <div class="button start-engagement-button css-api-widget-btn css-api-widget-btn--start" data-start-timer-up-count="">START</div>


When I do webscraping, I use one of two things:

  1. XPath
  • find_element_by_xpath
  1. CSS Selectors
  • find_element_by_css_selector

With Selenium, I remember having best success with using the XPath. If you are using Chrome, the element XPath is as easy to find as right-clicking an HTML element in the element tree -> Copy -> By XPath.

Hope this helps

This exception is thrown by various accessor methods to indicate that the element being requested does not exist . The next() method in Java returns the next element in the iteration or NoSuchElementException if the iteration has no more elements. The solution to this​ exception is to check whether the next position of an iterable is filled or empty.

As with most programming languages, the Iterator class includes a hasNext() method that returns a boolean indicating if the iteration has anymore elements. If hasNext() returns true, then the next() method will return the next element in the iteration otherwise raise exceptions if the iteration has no more elements.

if(input.hasNextInt() )
     number1 = input.nextInt(); // if there is another number  
     number1 = 0; // nothing added in the input