김은유
김은유

Reputation: 17

How to locate or extract texts in html file using selenium in python?

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.

Upvotes: 0

Views: 587

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193348

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:

Upvotes: 0

Sameer Arora
Sameer Arora

Reputation: 4507

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")

Upvotes: 1

Related Questions