zoukankan      html  css  js  c++  java
  • Selenium:frame嵌套页面元素定位(附页面源码+定位操作代码)

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

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

    Frame_test.html文件的源码如下:

     1 <html>
     2 <head>
     3 <title>Frame_test</title>
     4 </head>
     5 
     6 <body>
     7     <div>
     8         <iframe id="search" src="http://www.baidu.com" width="1200" height="700">
     9     </div>
    10 </body>
    11 
    12 <html>

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

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

     1 # #!/usr/bin/python3
     2 # -*- coding: utf-8 -*-
     3 # @Time : 2020/7/31 10:32
     4 # @Author : Gengwu
     5 # @FileName: Frame_test.py
     6 # @Software: PyCharm
     7 
     8 from selenium import webdriver
     9 from time import sleep
    10 
    11 driver=webdriver.Chrome()
    12 #设置网页文件路径
    13 file_path=r"D:pythonFrame.html" #r是路径转义,后面加上本地路径
    14 #路径转移的另外一种写法
    15 #file_path="D:\python\Frame.html"
    16 
    17 #将对应的路径加载进来,之前加的网页地址,现在加路径。路径名称file_path
    18 driver.get(file_path)
    19 #切换到frame页面
    20 driver.switch_to.frame("search") #search为frame源码里面的id值
    21 
    22 #定位到百度输入关键词
    23 driver.find_element_by_css_selector('#kw').send_keys('selenium') #通过id来定位
    24 sleep(2)
    25 driver.find_element_by_css_selector('#su')
    26 sleep(2)
    27 driver.quit()

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

    有问题欢随时讨论。

  • 相关阅读:
    Xshell学习第九课:数组与字符串
    Xshell学习第八课:函数
    Xshell学习第七课:sed语句
    Xshell学习第六课:read与for语句循环
    Xshell学习第五课:if判断语句
    Xshell学习第四课:grep与正则表达式
    Xshell学习第三课:编程原理
    Xshell学习第二课:重定向和管道符
    iOS中坐标转换
    iOS工作笔记(十五)
  • 原文地址:https://www.cnblogs.com/gengwulovestudy/p/13408690.html
Copyright © 2011-2022 走看看