博客
关于我
selenium 弹框处理机制
阅读量:754 次
发布时间:2019-03-23

本文共 1361 字,大约阅读时间需要 4 分钟。

警告框操作指南

在页面操作中,常常会遇到如alert、confirm、prompt等警告框。可以利用Selenium的switch_to.alert()方法来定位这些弹框,并进行相应操作。以下是常用方法详情:

  • switch_to.alert():用于定位当前页面上的警告框。
  • text:获取警告框中的文字信息。
  • accept():接受现有警告框(如点击“确定”)。
  • dismiss():关闭当前警告框(如点击“取消”)。
  • send_keys("警告内容"):向警告框中输入指定文本。
  • 这些方法为用户提供了对警告框操作的灵活控制,能够满足日常开发需求。

    操作示例

    from selenium import webdriverfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.common.by import Byclass TestAlert:    def setup(self):        self.driver = webdriver.Chrome()        self.driver.implicitly_wait(3)        self.driver.maximize_window()        def test_alert(self):        # 打开目标网页        self.driver.get("https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")        # 切换到弹框所在的框架        self.driver.switch_to.frame("iframeResult")        # 定位拖拽元素        draggable = self.driver.find_element(By.ID, 'draggable')        droppable = self.driver.find_element(By.ID, 'droppable')        # 拖拽操作        action = ActionChains(self.driver)        action.click_and_hold(draggable).move_to_element(droppable).release().perform()        # 等待弹框出现        sleep(3)        # 接受警告框        self.driver.switch_to.alert.accept()        # 切回默认内容框架        self.driver.switch_to.default_content()        # 点击提交按钮        self.driver.find_element_by_id("submitBTN").click()

    以上代码演示了如何通过Selenium处理页面中的警告框。操作流程包括:打开网页、定位元素、执行拖拽操作、接受警告框、最后点击提交按钮。整个流程简洁明了,便于理解和参考。

    转载地址:http://esczk.baihongyu.com/

    你可能感兴趣的文章
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>
    npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
    查看>>
    npm 下载依赖慢的解决方案(亲测有效)
    查看>>
    npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
    查看>>
    npm.taobao.org 淘宝 npm 镜像证书过期?这样解决!
    查看>>
    npm—小记
    查看>>
    npm介绍以及常用命令
    查看>>
    NPM使用前设置和升级
    查看>>
    npm入门,这篇就够了
    查看>>
    npm切换到淘宝源
    查看>>
    npm切换源淘宝源的两种方法
    查看>>
    npm前端包管理工具简介---npm工作笔记001
    查看>>
    npm包管理深度探索:从基础到进阶全面教程!
    查看>>
    npm升级以及使用淘宝npm镜像
    查看>>
    npm发布包--所遇到的问题
    查看>>
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>