专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

Selenium:frame嵌套页面元素定位(附页面源码+定位操作代码)

先看看内嵌页面长啥样哈:

120_1.png

案例:在Frame_test.html文件种定位百度搜索页面,进行搜索操作。

Frame_test.html文件的源码如下:

 <html>
 <head>
 <title>Frame_test</title>
 </head>

 <body>
     <div>
         <iframe id="search" src="http://www.baidu.com" width="1200" height="700">
     </div>
 </body>

 <html>

内嵌页面对应id元素查看截图如下:

120_2.png

嵌套页面代码和相关注释如下:

 # #!/usr/bin/python3
 # -*- coding: utf-8 -*-
 # @Time : 2020/7/31 10:32
 # @Author : Gengwu
 # @FileName: Frame_test.py
 # @Software: PyCharm

 from selenium import webdriver
 from time import sleep

 driver=webdriver.Chrome()
 #设置网页文件路径
 file_path=r"D:\python\Frame.html" #r是路径转义,后面加上本地路径
 #路径转移的另外一种写法
 #file_path="D:\\python\\Frame.html"

 #将对应的路径加载进来,之前加的网页地址,现在加路径。路径名称file_path
 driver.get(file_path)
 #切换到frame页面
 driver.switch_to.frame("search") #search为frame源码里面的id值

 #定位到百度输入关键词
 driver.find_element_by_css_selector('#kw').send_keys('selenium') #通过id来定位
 sleep(2)
 driver.find_element_by_css_selector('#su')
 sleep(2)
 driver.quit()

以上操作就可以定位到具体的内嵌啦。

有问题欢随时讨论。

文章永久链接:https://tech.souyunku.com/45898

未经允许不得转载:搜云库技术团队 » Selenium:frame嵌套页面元素定位(附页面源码+定位操作代码)

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们