Python Selenium无法访问Tableau小部件

问题描述

我正在尝试使用Python Selenium从this page提取数据。该表由Tableau呈现。我需要输入一些数据,然后使用下载按钮。

有趣的是,我无法从Selenium访问表中的元素。我尝试通过id,class或xpath查找。我不断收到NoSuchElementException。但是,这些元素是用HTML呈现的,我可以使用检查工具看到它们。有谁知道这是为什么,以及我如何使它们对Selenium可见?

EDIT1:这不是加载时间的问题。我尝试使用time.sleep(),并且也直接与页面进行交互。

解决方法

我可以看到您的表格位于iFrame中。首先进入内部,然后尝试抓取表格数据。

WebDriverWait(driver,20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src,'zika_Weekly_Agg_tben')]")))

# COde here to scrape  data

driver.switch_to.default_content() # To come out of frame

您需要导入

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
,

这非常具有挑战性,因为它有2个iframe,后跟阴影元素。并且不止于此。切换到iframe时,您没有iframe引用可用于访问shadow元素。您可以参考以下代码。它设法获得表格图表标题。

# Get first iframe and switch to it
root1 = driver.find_element_by_xpath("//div[@itemprop='articleBody']//iframe")
driver.switch_to.frame(root1)

# Grab the shadow element 
shadow = driver.execute_script('return document')

# Get the iframe inside shadow element of first iframe
iframe2 = shadow.find_element_by_xpath("//body/iframe")

# switch to 2nd iframe
driver.switch_to.frame(iframe2)
print("selected 2nd iframe")
shadow_doc2 = driver.execute_script('return document')
print("second iframe")
heading = shadow_doc2.find_element_by_xpath("//div[@class='tab-textRegion-content']/span//span[text()='Cases of Zika Virus Disease']/ancestor::div[2]").text
print(heading)

输出-

enter image description here

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...