首页 > 编程语言 >python selenium之JS滚动条处理

python selenium之JS滚动条处理

时间:2023-02-01 10:12:33浏览次数:34  
标签:execute script python selenium 元素 driver element 滚动条

在网页当中,页面存在滚动条,而你要操作的元素在当前屏幕可见区域之外。那么需要使用滚动条滚动到该元素处,然后再操作它。
selenium当中的使用execute_script方法执行js语句来实现滚动给你。
execute_script(“arguments[0].scrollIntoView();”,target)
其中target为find_element_by_xx找到的元素对象
几种滚动方式如下:

移动到元素element对象的“底端”与当前窗口的“底部”对齐: driver.execute_script(‘arguments[0].scrollIntoView(false);’,element)

移动到元素element对象的“顶端”与当前窗口的“顶部”对齐: driver.execute_script(‘arguments[0].scrollIntoView();’,element)
driver.execute_script(‘arguments[0].scrollIntoView(true);’,element)

 

移动到页面底部
driver.execute_script(“window.scrollTo(0,document.body.scrollHeight)”)

移动到页面顶部
driver.execute_script(“window.scrollTo(document.body.scrollHeight,0)”)
操作步骤:
1.通过selenium的查找元素方法先找到元素
2.通过执行js语句,将元素滚动到可见区域中

实例操作如下:

from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

driver = webdriver.Chrome()

driver.get("http://www.baidu.com")
driver.maximize_window()

element = driver.find_element_by_id("kw")   # WebElement对象 - 1个对象对应1个元素
element.send_keys("selenium webdriver")
driver.find_element_by_id('su').click()  # 带来了内容上的变化

# loc = (By.XPATH,'//div[@id="page"]//span[text()="2"]')
loc = (By.XPATH,'//a[text()="使用(一) - hello秘密花园 - ..."]')
WebDriverWait(driver,10).until(EC.visibility_of_element_located(loc))
a = driver.find_element(*loc)
print("测试元素")

##js函数-元素对象。scrollIntoView
# driver.execute_script('arguments[0].scrollIntoView(false);',a)
# a.click()

# 滚动到页面底部
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
time.sleep(4)

# 滚动到页面顶部
driver.execute_script("window.scrollTo(document.body.scrollHeight,0)")

 


————————————————
版权声明:本文为CSDN博主「杰森斯坦森1150」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/guiyin1150/article/details/108467577

标签:execute,script,python,selenium,元素,driver,element,滚动条
From: https://www.cnblogs.com/xujunhui/p/17081653.html

相关文章

  • logging --- Python 的日志记录工具
    logging ---Python的日志记录工具源代码: Lib/logging/__init__.pyImportant此页面仅包含API参考信息。教程信息和更多高级用法的讨论,请参阅基础教程进阶教......
  • python AttributeError: module 'matplotlib' has no attribute 'verbose'
    在pycharm中运行程序出现了该错误:AttributeError:module'matplotlib'hasnoattribute'verbose'  通过查询得知这其实不是程序的问题,也不是安装包的问题,是pycharm......
  • Selenium 隐藏浏览器指纹特征 转载
    转载自公众号 AirPython 大家好,我是安果!我们使用Selenium对网页进行爬虫时,如果不做任何处理直接进行爬取,会导致很多特征是暴露的对一些做了反爬的网站,做了特征检测,......
  • python UI自动化之JS定位
    前言本篇总结了几种js常用的定位元素方法,并用js点击按钮,对input输入框输入文本一、以下总结了5种js定位的方法除了id是定位到的是单个element元素对象,其它的都是elemen......
  • RabbitMq使用中常见错误--python版
    用python的pika库错误集 一、pika.exceptions.ProbableAuthenticationError:ConnectionClosedByBroker:(403)‘ACCESS_REFUSED-Loginwasrefusedusingauthentica......
  • python爬虫(三)- HTML解析之BeautifulSoup4
    BeautifulSoup可以从HTML、XML中提取数据。官网https://www.crummy.com/software/BeautifulSoup/官方中文文档https://www.crummy.com/software/BeautifulSoup/bs4/doc.......
  • Python 基础语法介绍(二)
    一、概述上一篇文章:Python基础语法介绍(一),已经介绍了一部分基础知识了,这里就继续介绍python基础知识。二、函数函数是组织好的,可重复使用的,用来实现单一,或相关联功能的......
  • 62复习Python_OOP
    私有属性与函数python中_相当于java的保护__相当于私有如果需要在外部使用对象._类名__实例属性/方法(python存在类属性和实例属性)类方法和静态方法类方法......
  • Python-数据结构
    数据结构:数据是一个抽象的概念,将其进行分类后得到程序设计语言中的基本类型。如int/float/char等。数据元素之间不是独立的,存在特点的关系,这些关系便是结构。数据结构指数......
  • Python Pillow(PIL) ImageFilter和ImageColor的使用
    Pillow(PIL)是Python平台事实上的图像处理标准库,支持多种格式,并提供强大的图形与图像处理功能。PIL模块全称为PythonImagingLibrary,是Python中一个免费的图像处理模块......