Home > database >  How do I get text that appear on page from div?
How do I get text that appear on page from div?

Time:05-27

The page source look like this:

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

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

CodePudding user response:

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
  • Related