0

How to locate or extract texts in a HTML file using Selenium in python. The text that I'm trying to get isn't an element.

<div class="datagrid row"> ==$0
 <h2 class="bottom-border block">Accepted Shipment</h2>
 <table>
  <tbody>
   <tr>
    <td>
     <input type="hidden" id="looseAcceptedPieces" value="56"> == $0
      " 56 pcs."

    <!--Modified by A-7558 for ICRD-244765 starts--> == $0
    <input type="hidden" id="acceptedWt" value> == $0
     "952 kg"

How do i locate or get that text under <input>, which is 56 pcs. and 952 kg perhaps, they are not elements.

2
  • You can read or take the entire html file as text file and then string search by "56 pcs" if "56 pcs" appears only under <input> tag. Commented Feb 28, 2020 at 5:38
  • How about collecting text under td?, It will give you 56 pcs., 952 kg in one string may be space-separated and than you can split it. Commented Feb 28, 2020 at 5:43

2 Answers 2

1

You can fetch the values by using get_attribute("value") method

piece = driver.find_element_by_id('looseAcceptedPieces')
val = piece.get_attribute("value")

And

weight = driver.find_element_by_id('acceptedWt')
val2 = weight.get_attribute("value")
Sign up to request clarification or add additional context in comments.

Comments

0

To extract the texts 56 pcs. and 952 kg as those are text nodes you need to induce WebDriverWait for the visibility_of_element_located() using execute_script() method and you can use either of the following based Locator Strategies:

  • To extract 56 pcs.:

    print(driver.execute_script('return arguments[0].childNodes[2].textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='Accepted Shipment']//following::table[1]/tbody/tr/td")))).strip())
    
  • To extract 952 kg:

    print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='Accepted Shipment']//following::table[1]/tbody/tr/td")))).strip())
    
  • Note : You have to add the following imports :

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

Reference

You can find a relevent discussion in:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.