1. 程式人生 > 實用技巧 >Selenium 模組2_iframe處理_動作鏈

Selenium 模組2_iframe處理_動作鏈

先例項化


from selenium import webdriver
#例項化一個瀏覽器物件,獲取網址
web_requests=webdriver.Chrome(executable_path='./chormedriver')
web_requests.get('')

iframe 動作鏈是什麼?

iframe 一個標籤
動作鏈 模擬人類的拖動的操作

如果定位的標籤是存在於iframe標籤之中 則必須通過如下操作在進行標籤定位

#鎖定iframe作用域
#switch_to.frame引數是id
web_requests.switch_to.frame('id')#切換瀏覽器標籤定位的作用域

動作鏈

from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
#例項化一個瀏覽器物件,獲取網址
web_requests=webdriver.Chrome(executable_path='./chormedriver')
web_requests.get('https:xxxx.com/')
#鎖定iframe作用域
#switch_to.frame引數是id
web_requests.switch_to.frame('id')#切換瀏覽器標籤定位的作用域
div=web_requests.find_element_by_id('draggable')

#動作鏈例項化
action=ActionChains(web_requests)
#點選長按指定標籤
action.click_and_hold(div)
for i in(range(5)):

    #分5次,每次向右移動17個畫素單位
    #move_by_offset(x,y)x水平 ,y豎直
    action.move_by_offset(17,0).perform()#perform()立即執行動作鏈操作
    #暫停一下0.2秒能看清楚
    sleep(0.2)
#釋放動作連結
action.release()