Search code examples
seleniumxpathcss-selectorstext-extractiondomxpath

xpath to extract the text in selenium


Need help in extracting the case id, would be great help

    <div class="note note-info"><h4 id="note-label-CreateCaseUploadDoc:Display_Process_Combination1:RequestID" class="note-title">A new request is created successfully</h4><p id="
    ">412312513</p></div></div>

Need to extract 412312513 out of this


Solution

  • You can use following-sibling to get text node value from p tag as follow:

    //*[@class="note-title"]/following-sibling::p
    

    OR

    Using css selector

    .note.note-info h4 + p
    

    Example with selenium

    txt = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@class="note-title"]/following-sibling::p'))).text
    

    OR selenium with css selector

    txt = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.note.note-info h4 + p'))).text
    

    #imports

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC