0

The page source look like this:

    <div class="caption">
                        <div id="question_speaker" style="display: none;"><img 
    src="../server/static/loudspeaker.png"></div>
                        <div class="translations">explain</div>
                        <div>&nbsp;</div>
                    </div>

I'm trying to get "explain" to text variable in python.

  • Hi. It is good practice to search before asking, how about this thread, might be helpfull... https://stackoverflow.com/questions/71899069/using-selenium-in-python-select-html-page-element-content-with-xpath :)) – Klemikaze May 27 '22 at 09:15
  • I already tried doing what I found before. I am probably making some stupid mistake in my code and I can't figure it out. – VespidWand20667 May 27 '22 at 09:28

1 Answers1

0

CSS:

div.caption div#question_speaker+div

XPATH:

//div[@class='caption']//div[@id='question_speaker']//following-sibling::div

Before using them make sure that they are unique in HTML-DOM

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath/css and see, if your desired element is getting highlighted with 1/1 matching node.

Code:

print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='caption']//div[@id='question_speaker']//following-sibling::div"))).text)

You'll need below import:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
cruisepandey
  • 28,520
  • 6
  • 20
  • 38