Selenium的崎岖定位问题

一、iframe框架定位问题

1、单个框架切换

1)切进框架

-- 可根据iframe的name或者ID定位;

举例:driver.switch_to.frame(‘frame’);

-- 根据元素定位,用元素的className或者css来定

如果没有name或者ID就根据元素对象到iframe元素;

举例:

 frame=driver.find_element(By.XPATH,'//iframe[@class="ke-edit-iframe"]')

 driver.switch_to.frame(frame)

--根据iframe的index来定位

 举例:driver.switch_to.frame (0); 表示第1个iframe

2)切出框架

框架元素识别完之后,一定要记得切回默认页面

driver.switch_to.default_content()   #切出当前框架,返回默认页面

2、多个框架切换

1)通过索引定位

# 通过索引定位区分

# 第一层框架

driver.switch_to.frame(0)  # 通过索引定位,通过插件数iframe的个数,从0开始

driver.find_element_by_xpath('//a[@id="switcher_plogin"]').click()

# 第二层框架

driver.switch_to.frame(1)

driver.find_element_by_xpath('//input[@id="login_button"]').click()

#返回上层框架

driver.switch_to.parent_frame()

#返回默认页

driver.switch_to.default_content()

2)通过元素定位

# 第一层框架

driver.switch_to.frame('ID或name')  # 也可以通过元素对象定位

driver.find_element_by_xpath('//a[@id="switcher_plogin"]').click()

# 第二层框架

driver.switch_to.frame('ID或name')

driver.find_element_by_xpath('//input[@id="login_button"]').click()

#返回上层框架

driver.switch_to.parent_frame()

#返回默认页

driver.switch_to.default_content()

待更新......

原文地址:https://www.cnblogs.com/HMeier/p/12682966.html