python selenium滚轮
时间: 2023-09-24 13:09:08 浏览: 94
要在Python Selenium中使用滚轮滚动页面,你可以使用execute_script函数来执行JavaScript代码来模拟滚轮动作。下面是三种不同的滚动页面的方法:
方法一:
使用document.documentElement.scrollTop属性将页面滚动到底部。具体代码如下:
```python
from selenium import webdriver
import time
web = webdriver.Chrome()
web.get("http://www.jd.com")
# 执行搜索操作
web.find_element_by_id('key').send_keys('背包')
web.find_element_by_class_name('button').click()
time.sleep(3)
# 向下偏移了10000个像素,到达底部
js = "var q=document.documentElement.scrollTop=10000"
web.execute_script(js)
time.sleep(5)
# 获取滚动后的页面元素
hah = web.find_elements_by_xpath('//*[@id="J_goodsList"]/ul/li')
print(len(hah))
web.close()
```
方法二:
使用scrollIntoView函数将元素滚动到可见区域。具体代码如下:
```python
from selenium import webdriver
web = webdriver.Chrome()
web.get("http://www.jd.com")
# 执行搜索操作
web.find_element_by_id('key').send_keys('背包')
web.find_element_by_class_name('button').click()
target = web.find_element_by_id("id_keypair")
# 将元素滚动到可见区域
web.execute_script("arguments
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)