# 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)
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
后跟shadow元素。而且还不止于此。切换到iframe
时,没有iframe
引用可用于访问shadow元素。您可以参考下面的代码。它设法得到了图表的标题输出-
我可以看到你的桌子在iFrame里面。首先进入内部,然后尝试刮取表数据
您需要导入
相关问题 更多 >
编程相关推荐