selenium页面滑动操作
滑动滚动条到某个指定的元素
一般经常使用到的就是滑动到页面的最顶部、最底部
- 滑动到页面的最顶部:“var q=document.documentElement.scrollTop=0”
- 滑动到页面的最底部:“window.scrollTo(0,document.body.scrollHeight);”
- 慢慢滑动至某个元素:以下代码是根据for循环,来滑动至某个元素位置(实际效果和功能测试一样,滑动到某个位置)
project = [xx, xxx, xx]
project_name = ['xx', 'xxx', 'xxx', 'xxx']
tab = [xxx, xxx, xxx, xx]
tab_name = ['xx数据', 'x数据', 'x数据', 'x数据']
try:
for i in range(len(tab)):
self.driver.find_element_by_xpath(tab[i]).click()
# 滑动元素至页面最底部
self.driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
ele= self.driver.find_element_by_xpath(tab[i])
js4 = "arguments[0].scrollIntoView();"
self.driver.execute_script(js4, ele)
time.sleep(3)
print('以下是【%s】数据' % tab_name[i])
for j in range(len(project)):
data = self.driver.find_element_by_xpath(project[j]).text
time.sleep(1)
ele = self.driver.find_element_by_xpath(project[j])
js4 = "arguments[0].scrollIntoView();"
# 滑动至指定的元素路径
self.driver.execute_script(js4, ele)
print("xx:%s xx金额:%s" % (project_name[j], data))
except Exception as msg:
print("%s" % msg)