how do I select the div that has this <i> using Selenium python

Question:

I want to scrape a quiz with selenium using python, here’s the code for 1 answer choice.

the will contains this specific class when it’s correct answer , and when its not correct it is another class for this

so I want to get the text of the question where the answer is correct (The product’s impact on customer satisfaction.)
It will depends of the class inside the < i > (because as I said, when its false answer, it’s another class inside )

But I can’t find how to select it with XPATH

<div class='course-player__interactive-checkbox _interactive-checkbox_cjbh9b'>
  <div class='course-player__interactive-checkbox__choice-label _interactive-checkbox__choice-label_cjbh9b'>
    <span class='course-player__interactive-checkbox__choice-letter _interactive-checkbox__choice-letter_cjbh9b'> ANSWER A </span>
  </div>
  <span class='course-player__interactive-checkbox__choice-text _interactive-checkbox__choice-text_cjbh9b'> The product's impact on customer satisfaction. </span>
  <span class='course-player__interactive-checkbox__choice-answered _interactive-checkbox__choice-answered_cjbh9b'>
    <i class='toga-icon toga-icon-circle-fill-check _interactive-checkbox__choice-icon_cjbh9b'> </i>
  </span>
</div>

Asked By: Ere06

||

Answers:

One approach to retrieve the string

The product’s impact on customer satisfaction.

with Selenium is the following:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("file:///input.html")
lookup = driver.find_elements_by_xpath("//div/span[i]/preceding-sibling::*[1]")
print(lookup[0].text)

The XPath-1.0 expression selects the <span> before the <span> which has an <i> child.
But be aware that this code does not contain any error handling.

Answered By: zx485
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.