【发布时间】:2019-01-21 03:30:21
【问题描述】:
我的 html 主页有一个 iframe,我需要获取那里的文本 Code: LWBAD。
查看图片以获得更好的理解:
下面是我的 主 html 页面源代码,其中包含一个 iframe:
<td class="centerdata flag"><iframe style="width: 200px; height: 206px;" scrolling="no" src="https://www.example.com/test/somewhere" ></iframe></td>
重定向链接(iframe 页面)有这个 html 源代码
<body>
<a href="http://www.test2.com" target="_blank">
<img src="https://img2.test2.com/LWBAD-1.jpg"></a>
<br/>Code: LWBAD
到目前为止,我可以从我的主 html 页面获得完整的页面源代码。
from bs4 import BeautifulSoup
from selenium import webdriver
import time
import html5lib
driver_path = '/usr/local/bin/chromedriver 2'
driver = webdriver.Chrome(driver_path)
driver.implicitly_wait(10)
driver.get('http://example.com')
try:
time.sleep(4)
iframe = driver.find_elements_by_tag_name('iframe')
driver.switch_to_default_content()
output = driver.page_source
print (output)
finally:
driver.quit();
*url 无法从我的网络外部访问,这就是我使用 example.com 的原因
【问题讨论】:
-
你不会在任何地方切换到
iframe。 -
@Guy 我是 Python 新手,你介意告诉我
switch to the frame应该去哪里吗?
标签: python html selenium iframe