Selenium+Python 学习笔记3

 1、通过link定位

# coding:utf-8
from selenium import webdriver
import time

#通过link定位 find_element_by_link_text
#通过partial link定位 find_element_by_link_text

url = "https://www.baidu.com/"
driver = webdriver.Firefox()
driver.get(url)
time.sleep(3)
# driver.find_element_by_link_text("hao123").click()

#一个很长的文本链接,如“hao123”,可以截取其中”hao1”部分
driver.find_element_by_link_text("hao1").click()

time.sleep(15)
driver.quit()

2、iframe切换

  只数有frame开头的,是第3个

# coding:utf-8
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://mail.126.com/")
time.sleep(3)

# 切换iframe
# 1.有id,并且唯一,直接写id
# driver.switch_to_frame("x-URS-iframe")
# # 2.有name,并且唯一,直接写name
# driver.switch_to_frame("xxxx")
# 3.无id,无name,先定位iframe元素
# iframe = driver.find_elements_by_tag_name("iframe")[0]
# driver.switch_to_frame(iframe)
# 4.通过index索引定位
# driver.switch_to_frame(2)  # 从0开始
#
# driver.find_element_by_name("email").send_keys("123456")
#
# # 退出iframe,再操作,操作完退出
# driver.switch_to_default_content()  # 回到主页面

# driver.switch_to_frame("f1")
# # 操作元素
# driver.switch_to_default_content()
# driver.switch_to_frame("f2")

# 嵌套
driver.switch_to_frame("f1")
driver.switch_to_frame("f2")
# driver.switch_to.parent_frame()  # 回到f1
driver.switch_to_default_content()  # 回到主页面

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章