• Python_Selenium之鼠标右键


    Python_Selenium之鼠标右键

    一、步骤:

    1. (以百度为例)获取百度网址
    2. 找到需要右键的元素(定位),xpath表达式为“//*[@id='lg']/img”
    3. 然后,右键选择“在新标签页中打开图片”即可,在这里需要用到selenium中的ActionChains模块

    二、ActionChains方法列表

    click(on_element=None) ——单击鼠标左键

    click_and_hold(on_element=None) ——点击鼠标左键,不松开

    context_click(on_element=None) ——点击鼠标右键

    double_click(on_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) ——松开某个键

    move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标

    move_to_element(to_element) ——鼠标移动到某个元素

    move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置

    perform() ——执行链中的所有动作

    release(on_element=None) ——在某个元素位置松开鼠标左键

    send_keys(*keys_to_send) ——发送某个键到当前焦点的元素

    send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素

    三、脚本代码如下:

    #coding:utf-8

    from selenium import webdriver

    from selenium.webdriver.common.keys import Keys

    from selenium.webdriver import ActionChains

    driver=webdriver.Chrome()

    driver.maximize_window()

    driver.implicitly_wait(8)

    driver.get("https://www.baidu.com/")#打开百度

    element=driver.find_element_by_xpath("//*[@id='lg']/img")#定位

    ActionChains(driver).context_click(element).click().perform()#右键,点击

    四、注意

    1. 试着用火狐和谷歌浏览器测试了一下,发现界面都只能停留在右键之后,出现菜单栏,但是没有执行后面的操作。

  • 相关阅读:
    Linux命令全训练
    解决maven中静态资源只能放到properties中的问题
    Mybatis出现错误org.apache.ibatis.executor.ExecutorException: No constructor found in
    Fence Repair
    Saruman's Army
    Best Cow Line
    区间调度问题
    硬币问题
    迷宫最短路径
    Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) A. Oath of the Night's Watch
  • 原文地址:https://www.cnblogs.com/Rita-LJ/p/7873658.html
Copyright © 2020-2023  润新知