selenium的使用(五)

移动网页滚动条

移动滚动条有三种方法。

第一种:

用ActionChains来模拟操作实现滑动滚动条。

ActionChains(browser).move_to_element().perform()

可以按F12 查看页面最后一个元素,然后定位元素,将鼠标移动到元素处,即可实现模拟移动。

第二种:

用ActionChains()中的模拟按键操作来进行。

#导入模块

from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.action_chains import ActionChains

#按键模拟

ActionChains(browser).key_down(Keys.DOWN).perform()

注: 一些网页会有下拉滚动条到底部,然后又会加载一部分。这种情况可以添加一个循环,就可以做到避免添加很多条语句。

例:

1 #循环
2 n = 0
3 while n < 1000:
4     ActionChains(browser).key_down(Keys.DOWN).perform()
5     n += 1
6 #for 循环
7 for x in range(1000):
8     ActionChains(browser).move_to_element(定位的元素).perform()

第三种:

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

driver.execute_script(js)   

这个方法我感觉不好用,建议读者每个都试一次,选适合自己的方法。

 

 

  有错误的地方还望各位给予指正,谢谢。

 

 

 

 

-------by sniper-huohuo--------

  ---------知耻而后勇---------

 

posted @ 2018-03-29 19:30  嚯嚯嚯  阅读(171)  评论(0编辑  收藏  举报