一、 可以通过iframe的name,id 或者 元素对象操作
#使用驱动 ,进入到指定的frame,注意,括号里写id
driver.switch_to.frame('abc')#进入到id为abc的frame里,再获取frame里的标签就可以了
二、 无name,id时,
考虑元素对象,通过标签等获取不了时,可以考虑使用xpath
Xpath参考:https://it-chengzi.blog.csdn.net/article/details/119696828
案例:
# 1.导入库
from selenium.webdriver import Chrome
url4 = 'http://okjx.cc/?url=https://v.qq.com/x/cover/m441e3rjq9kwpsc/w0040odz67k.html'
# 2.获得驱动
driver = Chrome(executable_path=r'D:\python\chromedriver_win32\chromedriver.exe')
# 3.1 使用驱动 ,打开网页
driver.get(url4)
#进入iframe
for i in range(4):
a = driver.find_element_by_xpath('//iframe[1]')
driver.switch_to.frame(a)
#获取链接
s = driver.find_element_by_tag_name('video')
print(s.get_property('src'))
#print(driver.page_source)
三、注意
1.executable_path驱动路径换成自己的。
2.iframe有几层嵌套,可以尝试使用循环几次,获取到最内容的内容。
#进入iframe
for i in range(4):
a = driver.find_element_by_xpath('//iframe[1]')
driver.switch_to.frame(a)