0
>     <div class="grid_8 alpha participant-info-fields">    
>                       <label for="grandTotal"><b>Grand Total:</b></label>
>                       $10,280.55
>                   </div>

This is the html that I am trying to get to the $ amount. I have tried //label[@for="grandTotal"]/child::node()

This only highlights the GrandTotal text. Since there is no attribute for the $ amount, I am not able to locate it.

PS: I can get to the node with the following: **//*[@id="planForm"]/div[3]/h3/div/text()** but this does not work since Selenium only works with Element nodes.

1 Answers1

0

The text $10,280.55 is within a text node. So to print the text you have to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using XPATH and childNodes:

    print(driver.execute_script('return arguments[0].lastChild.textContent;', WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'alpha participant-info-fields')][./label[@for='grandTotal']]")))).strip())
    
  • Using XPATH and splitlines():

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[contains(@class, 'alpha participant-info-fields')][./label[@for='grandTotal']]"))).get_attribute("innerHTML").splitlines()[4])
    
  • 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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    This works like a charm. However, for java I had to tweak a little bit. String test = driver.findElement(By.xpath("//div[contains(@class, 'alpha participant-info-fields')][./label[@for='grandTotal']]")).getAttribute("innerHTML").split()[3] – BornToBeAGamer Nov 20 '20 at 15:24
  • @BornToBeAGamer Oops, I assumed you were with Python. – undetected Selenium Nov 20 '20 at 15:31