0

I have HTML:

<div class="value">
    <div style="float: right;">100.00</div>
    <span class="yellow">Frequency:</span>
</div>

With selenium By.XPATH '//*[text()="Frequency:"]' I'm able to locate the <span> element. My goal is to get the innerText of the previous <div>.

Can I do it with selenium or should I use bs4 for this task?

I tried selenium.parent with documentation, but unable to do.

PS: I can't find the parent element or the div I need directly.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
T1mQa
  • 13
  • 3

1 Answers1

0

To print the text from the <div> tag i.e. 100.00 wrt the <span> you can use either of the following locator strategies:

  • Using xpath and get_attribute("innerHTML"):

    print(driver.find_element(By.XPATH, "//span[text()='Frequency:']//preceding::div[1]").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//span[text()='Frequency:']//preceding::div[1]").text)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352