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. 試着用火狐和谷歌瀏覽器測試了一下,發現界面都只能停留在右鍵之后,出現菜單欄,但是沒有執行后面的操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM