【问题标题】:.text. returns an empty string。文本。返回一个空字符串
【发布时间】:2020-07-26 10:20:03
【问题描述】:

尝试从 TradingView.com 获取当前 RSI 值,但 .text 行返回空字符串而不是值。谢谢。感谢所有帮助。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time


PATH = "/Applications/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://www.tradingview.com/chart/GZ2VoO8U/#signin")

# Sign into TradingView

time.sleep(2)
username = " "
password = " "
driver.find_element_by_xpath('//*[@id="overlap-manager- 
root"]/div/div[2]/div/div/div/div/div/div[1]/div[5]/div/div[1]/div/span').click()
driver.find_element_by_name('username').send_keys(username)
time.sleep(1)
driver.find_element_by_name('password').send_keys(password)
driver.find_element_by_class_name('tv-button__loader').click()
time.sleep(3)  # ensure the page loads (bottleneck)

#Search for the currency pair
driver.find_element_by_name("query").send_keys("AUDUSD", Keys.RETURN)

time.sleep(2)

Full_feature = driver.find_element_by_xpath('//*[@id="js-category- 
content"]/div/div/div/div/div[1]/div/div[1]/div/a')
Full_feature.click()

time.sleep(5)

Current_Rsi = driver.find_element_by_class_name("valueValue-3kA0oJs5")
print(Current_Rsi.text) #Returns an empty string

【问题讨论】:

  • 两个问题:1 你在找Stochastic RSI Fast (3, 3, 14, 14)吗? 2. 你能确认你从driver.find_element_by_xpath('//*[@id="overlap-manager- root"]/div/div[2]/div/div/div/div/div/div[1]/div[5]/div/div[1]/div/span').click()得到正确的值吗
  • 我正在寻找 RSI(14) 并且我试图获取的 xpath 是 driver.find_element_by_xpath('/html/body/div[2]/div[1]/div[2] /div[1]/div/table/tr[3]/td[2]/div/div[1]/div/div[2]/div[2]/div[3]/div/div/div' ) 但是,当我使用 xpath 时,它无法找到元素
  • 类名被多个技术人员使用,所以我必须恢复到返回 selenium.common.exceptions.NoSuchElementException 的 Xpath:消息:没有这样的元素:无法找到元素:{“方法” :"xpath","selector":"/html/body/div[2]/div[1]/div[2]/div[1]/div/table/tr[3]/td[2]/div /div[1]/div/div[2]/div[2]/div[3]/div/div"}
  • “valueValue-3kA0oJs5”类名下有很多标签可用。您可以尝试搜索元素而不是元素。之后,您可以遍历每个元素,然后尝试提取文本。此外,我是交易网站的新手,所以知道您到底在寻找什么。不知道什么是 RSI,以及它在您的页面中的位置。
  • 我尝试使用元素来查找类,但是,当我将变量放入 for 循环时,结果仍然是相同的空字符串。页面中的链接是我在打开新窗口时开始的登录页面,但如果您愿意,您可以搜索任何货币对以查看图表并添加指标以显示 rsi

标签: python selenium tradingview-api


【解决方案1】:

请在下面找到它正在提取一些文本的代码,但实际上我不确定这是否是您要查找的内容。我只是想帮忙。

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time

driver = webdriver.Chrome()
driver.get("https://www.tradingview.com/chart/GZ2VoO8U/#signin")

# Sign into TradingView

time.sleep(2)
username = " "
password = " "
driver.find_element_by_xpath(
    '//*[@id="overlap-manager-root"]/div/div[2]/div/div/div/div/div/div[1]/div[5]/div/div[1]/div/span').click()
driver.find_element_by_name('username').send_keys(username)
time.sleep(1)
driver.find_element_by_name('password').send_keys(password)
driver.find_element_by_class_name('tv-button__loader').click()
time.sleep(3)  # ensure the page loads (bottleneck)

# Search for the currency pair
driver.find_element_by_name("query").send_keys("AUDUSD", Keys.RETURN)

driver.implicitly_wait(10)
try:
    # This code executes when the browser is maximised.
    Full_feature = driver.find_element_by_xpath(
        '//*[@id="js-category-content"]/div/div/div/div/div[1]/div/div[1]/div/a')
    driver.execute_script("arguments[0].scrollIntoView();", Full_feature)
    ActionChains(driver).move_to_element(Full_feature).click(Full_feature).perform()
except:
    # This code executes when the browser is Restore down. (Small Browser)
    Full_feature = driver.find_element_by_xpath(
        '//*[@id="js-category-content"]/div/div/div/div/div[1]/div/div[1]/a')
    driver.execute_script("arguments[0].scrollIntoView();", Full_feature)
    ActionChains(driver).move_to_element(Full_feature).click(Full_feature).perform()

time.sleep(5)

print(driver.current_url)
Current_Rsi = driver.find_elements_by_class_name("valueValue-3kA0oJs5")

for myele in Current_Rsi:
    if myele.text != "": print(myele.text)

注意 - 如果这是您正在寻找的内容,请将此解决方案标记为答案。

【讨论】:

  • 感谢您的帮助,但结果并不是我想要的结果是目前位于 53.40 的 RSI,并且随着明天早上的市场开盘而不断变化。您返回的结果是当前价格和收盘后的变化。
  • 我正在查看这个网址 - tradingview.com/chart/?symbol=FX%3AAUDUSD。请让我知道 RSI 的位置和位置。
  • 顶部栏在搜索栏​​中有一个选项指标输入相对强度指数点击它,它应该出现在图表下方。 RSI 旁边的文本/数字是我要获取的文本。感谢您的帮助,非常感谢。
猜你喜欢
  • 2019-10-29
  • 2011-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多