How get the text from the <p> tag using XPath Selenium and Python How get the text from the <p> tag using XPath Selenium and Python selenium selenium

How get the text from the <p> tag using XPath Selenium and Python


The text() in the xpath is the issue here, see below:

import selenium.webdriver as webdriverbrowser = webdriver.Firefox()browser.get('https://www.w3.org/Protocols/rfc1341/7_1_Text.html')foo = browser.find_element_by_xpath('/html/body/p[5]')print(foo.text)


To print the text Content-type: text/plain; charset=us-ascii you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using XPATH and text attribute:

    driver.get("https://www.w3.org/Protocols/rfc1341/7_1_Text.html")print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[contains(., 'The charset parameter')]//following-sibling::p[2]"))).text)
  • Using XPATH and get_attribute():

    driver.get("https://www.w3.org/Protocols/rfc1341/7_1_Text.html")print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[contains(., 'The charset parameter')]//following-sibling::p[2]"))).get_attribute("innerHTML"))
  • Console Output:

    Content-type: text/plain; charset=us-ascii
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as EC