selenium切换frame(iframe)

例如网页代码为:
<html>
<<iframe src="myframetest.html" id="frame1" name="myframe"></iframe>>
<iframe id="frame2" / >
</iframe>
</html>


from selenium import webdriver
driver = webdriver.Firefox()
driver.switch_to.frame(0) # 1.用frame的index来定位,第一个是0
# driver.switch_to.frame("frame1") # 2.用id来定位
# driver.switch_to.frame("myframe") # 3.用name来定位
# driver.switch_to.frame(driver.find_element_by_tag_name("iframe")) # 4.用WebElement对象来定位
# driver.switch_to.frame(driver.find_element_by_xpath("//iframe[contains(@src,'myframe')]")) # 5.用xpath定位,传入WebElement对象


driver.switch_to.frame(frame1) # iframe一层一层的切入
driver.switch_to.parent_frame() # iframe退后操作,一层一层的退回
driver.switch_to.default_content() # 切换到主页面

猜你喜欢

转载自www.cnblogs.com/fanqian0330/p/10381904.html