无法检索网络元素对象

时间:2019-01-16 14:12:38

标签: python html

当我尝试提取investing.com历史数据中的表时,我无法检索列项目,并且发生错误

Traceback (most recent call last):

  File "<ipython-input-119-ba739f477693>", line 2, in <module>
    col = row.find_elements(By.TAG_NAME, "td")[0]

IndexError: list index out of range

rows = table_id.find_elements(By.TAG_NAME, "tr")

成功提取了表的所有行,但是当我尝试遍历每行并输出第一列项目时,发生了上述错误。

我进一步检查了第二行中的第一列项目是否在web元素下方

"selenium.webdriver.remote.webelement.WebElement (session="760e711c6189c07f6986103c1374ce13", element="0.5172974513607607-24")"

from selenium import webdriver

from selenium.webdriver.common.keys import Keys


browser = webdriver.Chrome(executable_path=r'chromedriver.exe')

browser.get("https://www.investing.com/commodities/brent-oil-historical-data")

table_id = browser.find_element(By.XPATH, '//*[(@id = "curr_table")]')

rows = table_id.find_elements(By.TAG_NAME, "tr")

for row in rows:

    col = row.find_elements(By.TAG_NAME, "td")[0] #e.g. get the first col

    print (col.text)

1 个答案:

答案 0 :(得分:0)

您可以使用以下方式获取元素。我刚刚改变了您获取表和行的方式。

return !set.contains { $0.id == entry.id }