skip to Main Content

I’m attempting to extract "479" from this sample HTML:

<div data-testid="testid">
  "479"
  " Miles Away"
</div>

I’m using the following Selenium code in Python:

xpath = 'html/body/div/text()[1]'
WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, xpath)))
distance = driver.find_element(By.XPATH, xpath)
print(distance)

Which returns the following error:

'The result of the xpath expression "html/body/div/text()[1]" is: [object Text]. It should be an element.'

I’ve attempted to remove text()[1] from the end of my xpath, theoretically printing off all data contained the in the HTML div, but it will instead print a blank line when I do so.

Note: I’m an amateur and self-taught (via mostly Google, YouTube, and this site), so some of my wordage may not be correct. I apologize in advanced.

4

Answers


  1. The problem is that you can’t treat text like that, the text() function returns everything as a string including a line break. I think there is no split function that can help you with that, I advise you to get the text in a python variable and do a split(‘n’) to the text.

    xpath = 'html/body/div/text()'
    WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH, xpath)))
    distance = driver.find_element(By.XPATH, xpath)
    print(distance.split('n')[0])
    
    Login or Signup to reply.
  2. You should take the entire element (without text()) using only

    html/body/div
    

    then from returned element get text, which will be: "479" " Miles Away" .
    Then using split method from python you can take that number(split by n, space, or ").

    Login or Signup to reply.
  3. Selenium doesn’t support the following xpath
    xpath = 'html/body/div/text()[1]'

    To identify the element uniquely, Your xpath should be like

    xpath = '//div[@data-testid="testid"]'
    WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, xpath)))
    distance = driver.find_element(By.XPATH, xpath).text
    print(distance)
    

    To get the text of the element you have to use element.text

    Login or Signup to reply.
  4. Given the html:

    <div data-testid="testid">
      "479"
      " Miles Away"
    </div>
    

    Both the texts 479 and Miles Away are with in 2 different text nodes.

    Selenium doesn’t supports text() as it returns a text node, where as Selenium expects back a WebElement. Hence you see the error:

    The result of the xpath expression "html/body/div/text()[1]" is: [object Text]. It should be an element.
    

    Solution

    To extract the text 479 you can use either of the following locator strategies:

    • Using xpath through execute_script() and textContent:

      print(driver.execute_script('return arguments[0].firstChild.textContent;', WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//div[@data-testid='testid']")))).strip())
      
    • Using xpath through splitlines() and get_attribute():

      print(WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//div[@data-testid='testid']"))).get_attribute("innerHTML").splitlines()[1])
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search