如何通过Selenium在iframe中检索html
如果html如下所示,我希望在iframe标记下获取所有html内容(示例中为all xxxx):如何通过Selenium在iframe中检索html,selenium,selenium-webdriver,iframe,webdriverwait,Selenium,Selenium Webdriver,Iframe,Webdriverwait,如果html如下所示,我希望在iframe标记下获取所有html内容(示例中为all xxxx): <body> <div></div> .... <div class = A> <div class=B> <div class = C> <iframe class = D> xxxxxxx </iframe> </div>
<body>
<div></div>
....
<div class = A>
<div class=B>
<div class = C>
<iframe class = D>
xxxxxxx
</iframe>
</div>
</div>
</div>
我试过这样的代码,这个代码有什么问题吗?错误消息是:
错误消息:
Unable to find element with xpath
对于这样的HTML:
<html>
<head>
<title>
Stack over flow
</title>
</head>
<body>
<p>This ius </p>
<iframe class='D'></iframe>
<p><a href="https://www.w3schools.com" target="iframe_a">W3Schools.com</a></p>
<html>
<head>
<title>
Stack over flow iframe
</title>
</head>
<body>
<p>This is 1 </p>
<p>This is 2 </p>
<p>This is 3 </p>
</body>
</iframe>
</body>
</html>
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome(executable_path = r'C:/Users/abhishep/Downloads/chromedriver_win32/chromedriver.exe')
driver.maximize_window()
wait = WebDriverWait(driver,40)
driver.get("C:\\Users\\User***\\Desktop\\Python+Selenium\\SO.html")
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.D")))
print(driver.page_source)
source = driver.execute_script("return document.body.innerHTML;")
print(source)
根据您在尝试获取
标记下的所有HTML内容时提供的HTML,逻辑上,
中应该有一些您希望与之交互的元素。因此,首先您必须诱导WebDriverWait使框架可用并切换到它,然后再次诱导WebDriverWait使所需元素可见(可交互),然后您可以按如下方式提取整个源代码:
#WebDriverWait for the desired frame to be available and switch to it
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='D']")))
#WebDriverWait for the desired element to be visible
WebDriverWait(driver, 10).until(EC.visibilityOfElementLocated((By.XPATH, "xpath_of_desired_element")))
print(driver.page_source)
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
你需要先切换到框架,然后把所有元素都放进去,这样有帮助吗?
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC