Home > Software design >  Can't get element value by css or xpath
Can't get element value by css or xpath

Time:06-11

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 > [flex]
  <div >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?

CodePudding user response:

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)
    
  • Related