PS:滚动屏幕
代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019-07-15 16:12:33
# @Author : BenLam
# @Link : https://www.cnblogs.com/BenLam/
from selenium import webdriver
driver=webdriver.Firefox()
driver.get(r‘test.html‘)
# 向下移动 500 个像素点,代表x, y轴
driver.execute_script("window.scrollBy(0,500)")
# 同上,下降 1000 个像素点
# PS:不同的浏览器,可能或存在兼容问题
driver.execute_script("window.scrollBy(0,1000)")
driver.quit()
PS: 滚动屏幕并定位元素, 这里我们打开博客园,定位到“统计信息”栏
代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2019-07-15 16:12:33
# @Author : BenLam
# @Link : https://www.cnblogs.com/BenLam/
from selenium import webdriver
driver=webdriver.Firefox()
driver.get(r‘https://www.cnblogs.com/‘)
# 先找元素
a = driver.find_element_by_xpath(r‘//*[@id="side_nav"]/div[7]/h4‘)
# 通过 js 方式找滚动到元素所在地方
# 打印 text 显示为 “‘统计信息‘”
text = driver.execute_script("return arguments[0].scrollIntoView();", a)
driver.quit()
原文:https://www.cnblogs.com/BenLam/p/11188714.html