Get startedGet started for free

Where it's @

In this exercise, you'll begin to write an XPath string using attributes to achieve a certain task; that task is to select the paragraph element containing the text "Thanks for Watching!". We've already created most of the XPath string for you.

Consider the following HTML:

<html>
  <body>
    <div id="div1" class="class-1">
      <p class="class-1 class-2">Hello World!</p>
      <div id="div2">
        <p id="p2" class="class-2">Choose DataCamp!</p>
      </div>
    </div>
    <div id="div3" class="class-2">
      <p class="class-2">Thanks for Watching!</p>
    </div>
  </body>
</html>

We have created the function print_element_text() for you, which will print any text contained in your element.

This exercise is part of the course

Web Scraping in Python

View Course

Exercise instructions

  • Fill in the blanks in the XPath string to select the paragraph element containing the phrase: "Thanks for Watching!".

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create an Xpath string to select desired p element
xpath = '//*[@id=____]/p'

# Print out selection text
print_element_text( xpath )
Edit and Run Code