python+selenium自动化测试-6操作富文本框iframe

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_24601279/article/details/102605528

富文本的常见技术用到了iframe标签,并且在iframe里面实现了一个完整的HTML网页结构,使用普通定位模式将无法直接定位到富文本框对象。

frame与iframe两者可以实现的功能基本相同,不过iframe比frame具有更多的灵活性。 frame是整个页面的框架,iframe是内嵌的网页元素,也可以说是内嵌的框架。iframe和frame最大区别是在iframe所包含的内容与整个页面是一个整体,而frame所包含的内容是一个独立的个体。

1、iframe有id或name

#获取id为ueditor_0的frame

iframe=self.dirver.find_element_by_xpath(‘//iframe[contains(@id,”ueditor_0”)]’)

#切换进入富文本框中

self.driver.switch_to.frame(iframe)

#从富文本框中切换出,回到默认页面

self.driver.switch_to.default_content()

#通过JavaScript代码向邮件正文编辑框中输入正文

self.driver.execute_script(“document.getElement_by_tag_name(‘body’)[0].innerHTML=’<b>邮件正文内容<b>’”)

2、没有id或name

sendText_loc = (By.CLASS_NAME,"cke_editable.cke_editable_themed.cke_contents_ltr.cke_show_borders")#用于定位富文本框

newsContent_loc = (By.XPATH,"//form[@name='add']/div/div/div/div/iframe")#定位iframe

iframe=self.find_element(*self.newsContent_loc)self.driver.switch_to.frame(iframe)#切换到iframe

self.find_element(*self.sendText_loc).send_keys(self.randoma)

self.driver.switch_to.default_content()#切换到默认文档

猜你喜欢

转载自blog.csdn.net/qq_24601279/article/details/102605528
今日推荐