首页 > 其他分享 >Selenium-ActionChains类

Selenium-ActionChains类

时间:2024-06-04 17:56:46浏览次数:10  
标签:option ActionChains Selenium 元素 driver element 鼠标

一、介绍

  • move_to_element(element):将鼠标移动到指定的元素上。
  • move_to_element_with_offset(element, xoffset, yoffset):将鼠标移动到指定元素的偏移位置。
  • click(element=None):单击指定的元素。如果未指定元素,将在当前鼠标位置单击。
  • click_and_hold(element=None):按住指定元素。如果未指定元素,将在当前鼠标位置按住。
  • double_click(element=None):双击指定的元素。如果未指定元素,将在当前鼠标位置双击。
  • context_click(element=None):右击指定的元素。如果未指定元素,将在当前鼠标位置右击。
  • drag_and_drop(source, target):将源元素拖拽到目标元素上。
  • drag_and_drop_by_offset(source, xoffset, yoffset):将源元素拖拽到指定的偏移位置。
  • key_down(value, element=None):按住指定的键。如果提供了元素,将在元素上按住键。
  • key_up(value, element=None):释放指定的键。如果提供了元素,将在元素上释放键。
  • send_keys(*keys_to_send):在当前鼠标位置的元素上发送按键。
  • send_keys_to_element(element, *keys_to_send):在指定元素上发送按键。
  • pause(seconds):暂停指定的秒数。
  • perform():执行ActionChains中的所有操作。

 

二、引用

from selenium.webdriver import ActionChains

 

三、使用示例

1、访问高德地图:https://www.amap.com/

 

2、点击北京

 

3、示例代码

# -*- coding: utf-8 -*-
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver import ActionChains
from selenium import webdriver
import os

os.system("taskkill /f /im chromedriver.exe")  # 结束之前运行的chromedriver进程
option = webdriver.ChromeOptions()  # 创建Chrome选项对象
option.add_experimental_option('useAutomationExtension', False)  # 禁用扩展
option.add_argument('ignore-certificate-errors')  # 忽略证书错误
option.add_experimental_option("detach", True)  # 解决代码执行完成后,浏览器会自动关闭问题
option.add_argument('--disable-dev-shm-usage')  # 大量渲染时候写入/tmp而非/dev/shm
option.add_experimental_option("excludeSwitches", ['enable-automation'])  # 排除enable-automation开关
option.add_argument("disable-blink-features=AutomationControlled")  # 禁用Blink特性

driver = webdriver.Chrome(options=option)  # 创建Chrome浏览器实例
driver.maximize_window()  # 最大化窗口
driver.get("https://www.amap.com")  # 访问高德地图网站
sleep(5)  # 等待5秒,确保页面加载完成

class Ac():

    gaode_ly_loc = (By.ID, "renderArrowLayer")
    
    def gd(self, driver):
        sleep(1)
        ly = driver.find_element(*self.gaode_ly_loc)  # 查找元素

        x, y = 961, 480  # 定义偏移位置

        try:

            actions = ActionChains(driver)  # 创建ActionChains对象
            actions.move_to_element_with_offset(ly, x, y)  # 将鼠标移动到指定元素的偏移位置
            actions.pause(3)  # 暂停2秒
            actions.click()  # 单击
            actions.perform()  # 执行操作

            # ActionChains(driver).move_to_element_with_offset(ly, x, y).pause(2).click().perform()

            print("T")
        except:
            print("F")

# 创建Ac类实例并调用gd方法
Ac().gd(driver)

 

4、实现后效果

 

5、Page Ruler  点击进行下载

 

标签:option,ActionChains,Selenium,元素,driver,element,鼠标
From: https://www.cnblogs.com/TSmagic/p/18197911

相关文章

  • Python使用selenium库实现网页自动化登陆以及数据抓取(爬虫)教程
      相比于传统的网络请求实现爬虫,Selenium可以模拟用户在浏览器上的操作,处理由JavaScript生成的动态内容,以执行点击、滚动、表单提交等操作,模拟真实用户访问,绕过一些反爬机制,更方便的获取动态生成的网站数据。  本篇教程将采取环境搭建、自动化操作、使用cookie自动化登陆、实......
  • zf_利用feapder中的selenium网页爬取数据
    "http://www.ccgp.gov.cn/cggg/dfgg/"#个人学习用切勿其他用途#标题name#发布时间publish_time#地域location#采购人purchaser#采购网址url#前提配置数据库安装feapder库importrandomimportreimporttimeimportfeapderfromfeapder.utils.webdriverimportWebDrive......
  • Selenium运行后没反应
    问题今天在练习自动化的时候,运行后浏览器一直不调用,要不就得等好久才会打开浏览器(已经配置了chromedriver的情况下)。一直显示如下画面,重装浏览器后问题也得不到解决。 解决方案selenium的版本有问题,在pycharm中把selenium的版本减低,我这里降低到4.1.1后问题解决.  ......
  • feapder通过selenium爬取网页数据-用到yield关键字可循环爬取
    importrandomimporttimeimportfeapderfromfeapder.utils.webdriverimportWebDriverfromparselimportSelectorfromfeapder.db.mysqldbimportMysqlDBfromselenium.webdriver.common.byimportByclassTestRender(feapder.AirSpider):db=MysqlDB()__cust......
  • 【Python爬虫--scrapy+selenium框架】超详细的Python爬虫scrapy+selenium框架学习笔记
    六,selenium想要下载PDF或者md格式的笔记请点击以下链接获取python爬虫学习笔记点击我获取Scrapy+selenium详细学习笔记点我获取Python超详细的学习笔记共21万字点我获取1,下载配置##安装:pipinstallselenium##它与其他库不同的地方是他要启动你电脑上的浏览器......
  • python selenium mysql -- 数据爬取2
    fromseleniumimportwebdriverfromselenium.webdriver.common.byimportBydriver=webdriver.Chrome()driver.get('https://www.00ksw.com/html/120/120704/')zj_list=[]#使用更健壮的XPath或CSS选择器links=driver.find_elements(By.XPATH,'/html/bo......
  • Selenium Python 无法找到仅有类的定义不清的网络按钮
    我拥有的HTML元素是YesCancel我想点击"是"按钮,因此我尝试了以下3种方法yes_driver2=driver.find_element("xpath",'//button[text()="Yes"]')yes_driver2.click()yes_driver3=driver.find_elements(By.XPATH,'//button[contains(text(),&q......
  • Python selenium webdriver
    元素定位第一步,导入selenium模块的webdrivier包importtimefromseleniumimportwebdriverfromselenium.webdriver.common.byimportBy窗口最大化driver=maximize_window()第二步,调用webdriver包的Chrome类,返回chrome浏览器对象driver=webdriver.Chrome()第三步,如使用......
  • selenium模拟登录网站
     importtimeimportrequestsfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBybrowser=webdriver.Edge()browser.maximize_window()url="https://m.xiaoshuopu.com/user/login.html"browser.get(url)username_input=b......
  • 如何隐藏 Firefox 窗口(Selenium WebDriver)?
    在Python中使用SeleniumWebDriver隐藏Firefox窗口通常涉及到配置FirefoxOptions来禁用其图形界面的显示。以下是一个详细的步骤和代码示例:1.首先,确保你已经安装了selenium库,以及geckodriver(适用于Firefox浏览器)。如果还没有安装,可以通过pip进行安装:```bashpipinstallsel......