selenium iframe

  来源  http://blog.csdn.net/huilan_same/article/details/52200586

    switch_to.frame(参数)

    参数可以传入id、name、index以及selenium的WebElement对象。


    如下代码:  

    <html lang="en">
    <head>    
	<title>FrameTest</title>
    </head>
    <body>
    <iframe src="a.html" id="frame1" name="myframe"></iframe>
    </body>
    </html>

     from selenium import webdriver

     driver  = webdriver.Chorme()

     driver.switch_to.frame(0)  # index = 0

     driver.switch_to.frame("frame1")  # id

扫描二维码关注公众号,回复: 5977538 查看本文章

     driver.switch_to.frame("myframe")  # name

     driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))  # WebElement对象

     

    

采用id和name就能够解决绝大多数问题。但有时候frame并无这两项属性,则可以用index和WebElement来定位:

  • index从0开始,传入整型参数即判定为用index定位,传入str参数则判定为用id/name定位
  • WebElement对象,即用find_element系列方法所取得的对象,我们可以用tag_name、xpath等来定位frame对象
     从frame切回主文档
     driver.switch_to.default_content()

     子frame切回到父frame
      driver.switch_to.parent_frame()

    


   

猜你喜欢

转载自blog.csdn.net/vany_/article/details/78667098