简体   繁体   中英

Can't get element value by css or xpath

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM