简体   繁体   English

无法通过 css 或 xpath 获取元素值

[英]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 resultsfind_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.

相关问题 无法使用 lxml xpath 获取 xml 元素值 - Can't get the xml element value using lxml xpath 无法从xpath python获取价值 - Can't get value from xpath python 无法通过 xpath 或 css 选择器找到文本框元素 - can't find text box element by xpath or css selector Css 或 Xpath 选择器不能 select 列表元素 - Css or Xpath selector can't select a list element Selenium 在无头模式下无法通过 xpath 获取元素 Ubuntu 18.04 - Selenium can't get element by xpath in headless mode Ubuntu 18.04 无法使用 Selenium 通过 xpath 获取带有查找元素的文本 - Can't get Text with Find Element by xpath with Selenium 尽管使用了正确的 CSS 选择器/XPATH 并且我正在抓取的 html 中没有 iframe,但我无法找到一个元素。 我如何获得元素? - I can't locate an element despite using correct CSS selector/XPATH and there is no iframe in html that I'm scraping. How do I get the element? 使用xpath无法使用Selenium RC获取属性的值 - Can't get the value of an attribute with Selenium RC using xpath Selenium Python - 在深度嵌套的 HTML 元素中找不到正确的 CSS 或 Xpath - Selenium Python - Can't Find Correct CSS or Xpath in deeply nested HTML Element 无法<span>使用 xpath 或 css 选择器在网站上</span>定位元素 - can't locate element behind <span> on a website using xpath or css selector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM