python爬虫之iframe处理+动作链

python爬虫之iframe处理+动作链

selenium处理iframe

1、如果定位的标签存在于iframe标签之中,则必须使用switch_to.frame(id)
2、动作链(拖动):from selenium.webdriver import ActionChains
(1)实例化一个动作链对象:action = ActionChains(bro)
(2)click_and_hold(div):长按且点击操作
(3)move_by_offset(x,y)
(4)perform()让动作链立即执行
(5)action.release()释放动作链对象
需求:将方框拖入另一个方框中
1、拖动前
在这里插入图片描述
(2)拖动后
在这里插入图片描述
实现代码如下:

#需求:拖动方块到指定位置
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
#导入动作链对应的类
from selenium.webdriver import ActionChains

from selenium.webdriver.edge.options import Options
edge_options = Options()
edge_options.add_argument("--guest") # 启用guest模式。
# edge_options.add_experimental_option("detach",True)
# 创建一个Edge WebDriver实例
bro = webdriver.Edge(options=edge_options)

# option = webdriver.EdgeOptions()
# option.add_experimental_option("detach", True)
#
# bro = webdriver.Edge(options=option)
bro.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')


#如果定位的标签是存在于iframe标签之中的则必须通过如下操作在进行标签定位
bro.switch_to.frame('iframeResult')#切换浏览器标签定位的作用域
div = bro.find_element(By.ID,'draggable')

#动作链
action = ActionChains(bro)
#点击长按指定的标签
action.click_and_hold(div)

for i in range(5):
    #perform()立即执行动作链操作
    #move_by_offset(x,y):x水平方向,y竖直方向
    action.move_by_offset(55,0).perform()
    sleep(0.3)

#释放动作链
action.release()

bro.quit()
  • 16
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值