Can't get element value by css or xpath

Issue

i tried to get the value of an element by xpath and css, and tried with .text and .get_attribute(‘value’), but no way to get it.

element:

<div class="itemWrapper-tp4JSoHa"> [flex]
  <div class="value-tp4JSoHa">1205.61</div

code 1:

WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='value-tp4JSoHa']")))
equity = driver.find_element_by_xpath("//*[@class='value-tp4JSoHa']")
num_equity = equity.text #int(equity.text) 
return num_equity

RESULT = 0

code 2:

WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='value-tp4JSoHa']")))
equity = driver.find_element_by_css_selector('div.itemWrapper-tp4JSoHa:nth-child(2) > div:nth-child(1)').get_attribute('value') 
return equity

RESULT = NONE

Any solutions?

Solution

Referring to your code 1 example:

  • Do the "clever" trick of replacing find_element_by_xpath with find_elements_by_xpath (a minor difference) that now returns a list of results

  • Now either iterate over the list, or select the first element with the index [0]:

    num_equity = equity[0].text #int(equity.text) 
    

    which gives you 1205.61

  • And if you want to return the count of elements in the list, use

    num_equity = len(equity)
    

Answered By – zx485

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published