[英]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.我试图通过 xpath 和 css 获取元素的值,并尝试使用 .text 和 .get_attribute('value'),但没有办法得到它。
element:元素:
<div class="itemWrapper-tp4JSoHa"> [flex]
<div class="value-tp4JSoHa">1205.61</div
code 1:代码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结果 = 0
code 2:代码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:参考您的代码 1 示例:
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用find_elements_by_xpath
替换find_element_by_xpath
的“聪明”技巧(一个微小的区别)现在返回结果列表
Now either iterate over the list, or select the first element with the index [0]
:现在要么遍历列表,要么选择索引为[0]
的第一个元素:
num_equity = equity[0].text #int(equity.text)
which gives you 1205.61
这给了你1205.61
And if you want to return the count of elements in the list, use如果要返回列表中元素的计数,请使用
num_equity = len(equity)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.