sky_cheng

导航

 

UI自动化中经常会遇到元素识别不到,找不到的问题,原因有很多,比如不在iframe里,xpath或id写错了等等;但有一种是在当前显示的页面元素不可见,拖动下拉条后元素就出来了。

在python中有几种方法解决这种问题

一、使用js脚本直接操作

js="var q=document.getElementById('id').scrollTop=10000"

driver.execute_script(js)

或:

js="var q=document.documentElement.scrollTop=10000"

driver.execute_script(js)

这里的id为滚动条的id,但js中没有xpath的方法,所以滚动条没有id的网页此方法不适用

二、使用js脚本将滚动条拖动到指定元素地方

accounting_unit_element = self.brower.find_element_by_xpath('//span[text()="考核单元"]')

self.brower.execute_script('arguments[0].scrollIntoView();',accounting_unit_element)

这种方式,会将accounting_unit_element元素显示到当前frame的顶部位置

三、发送PAGE_DOWN、END等键盘事件

END:可以让页面直接下拉到底

HOME:上拉到顶端

PAGE_DOWN:小幅度下拉

PAGE_UP:小幅度上拉

accounting_unit_element = self.brower.find_element_by_xpath('//span[text()="考核单元"]')

action=ActionChains(self.brower)

action.send_keys(Keys.PAGE_DOWN).perform()

四、滚动条滚动方法

#下拉到页面底部

driver.executeScript('window.scrollTo(0,document.body.scrollHeight);')

#上拉到页面顶端

driver.executeScript('window.scrollTo(document.body.scrollHeight,0);')

#下拉到页面1000位置

driver.executeScript('window.scrollTo(0,1000);')

#上拉到页面顶端 0,0位置

driver.executeScript('window.scrollTo(0,0)';)

#滚动到距离当前位置的0,100位置(向下滚动)

driver.executeScript('window.scrollBy(0,100)';)

posted on 2021-03-18 09:31  sky_cheng  阅读(390)  评论(0编辑  收藏  举报