Get startedGet started for free

All Level Text

This exercise is similar to the previous, but differs in that you will be selecting text from multiple generations of a given element.

You will write an XPath and CSS Locator strings to direct to the text of a specific paragraph p element. The p element in the HTML is uniquely defined by its id attribute, which is "p3". With this small piece of information, you should be able to create the desired strings; however, we have preloaded the variable html with a string containing the HTML in which this link belongs, if you want to peruse it.

In this exercise, you will only be selecting the text within the element which includes all text within the future generations. We have created a function print_results for you to compare which elements your strings direct to.

This exercise is part of the course

Web Scraping in Python

View Course

Exercise instructions

  • Assign to the variable xpath an XPath string directing to the text within the paragraph p element with id equal to p3, which includes the text of future generations of this p element.
  • Assign to the variable css_locator a CSS Locator string directing to this same text.

Hands-on interactive exercise

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

# Create an XPath string to the desired text.
xpath = ____

# Create a CSS Locator string to the desired text.
css_locator = ____

# Print the text from our selections
print_results( xpath, css_locator )
Edit and Run Code