【发布时间】:2019-07-12 10:29:17
【问题描述】:
我想编写一个 python 程序,它可以从网页自动下载历史股票数据。我想选择的元素对应的 HTML 代码如下图所示:
有两个 iframe。一个在另一个里面。我切换到第二个 iframe,但找不到我要单击的元素。我收到以下错误:“消息:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“[id =”:cu“]”}(会话信息:chrome = 75.0 .3770.100)"
from requests import get
from requests.exceptions import RequestException
from contextlib import closing
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import ctypes # An included library with Python install.
import time
user = ""
pwd = ""
driver = webdriver.Chrome()
driver.get("https://www.dukascopy.com/trading-tools/widgets/quotes/historical_data_feed")
driver.maximize_window()
## Give time for iframe to load ##
time.sleep(1)
# get the list of iframes present on the web page using tag "iframe"
seq = driver.find_elements_by_tag_name('iframe')
print("No of frames present in the web page are: ", len(seq))
#switch to correct iFrame
driver.switch_to_default_content()
iframe = driver.find_elements_by_tag_name('iframe')[1]
driver.switch_to.frame(iframe)
driver.implicitly_wait(5)
elem = driver.find_element_by_id(':cu')
elem.click()
ctypes.windll.user32.MessageBoxW(0, "Test", "Test MsgBox", 1)
driver.close()
如果我的代码正确,则列表中的元素“EUR/TRY”将被选中。
【问题讨论】:
-
应该打印
print("No of frames present in the web page are: ", len(seq))4 ? -
无论如何你想从页面中提取什么?
-
我想下载历史股票数据。