0

I have HTML tags as below:

<div class="mt-md common__EiReviewTextStyles__allowLineBreaks">
    <p class="strong">Pros</p>
    <p>This is the text to be captured</p></div>

I want to access the text " This is the text to be captured " using selenium on Python.

Any ideas on how this can be done?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user3021495
  • 97
  • 10

2 Answers2

0

Use .find_element_by_xpath, like bellow:

driver.find_element_by_xpath('//div[contains(@class, "mt-md")]//p[@class="strong"]//following-sibling::p[text()]').text

Or if you want achieve all text in your div, use .find_element_by_css_selector:

driver.find_element_by_css_selector('div.mt-md.common__EiReviewTextStyles__allowLineBreaks').text
frianH
  • 7,295
  • 6
  • 20
  • 45
0

To extract the text within the second <p> tag i.e. This is the text to be captured you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and get_attribute():

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.mt-md.common__EiReviewTextStyles__allowLineBreaks p:nth-of-type(2)"))).get_attribute("innerHTML"))
    
  • Using XPATH and text:

    print(WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='mt-md common__EiReviewTextStyles__allowLineBreaks']//following-sibling::p[2]"))).text)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352