python3.6+selenium操作JavaScript弹窗

本文介绍了如何在Python3.6中使用selenium库来处理JavaScript弹窗,通过示例代码展示了捕获和处理alert对话框的方法,包括accept()和dismiss(),并解决了解决switch_to_alert()报错的问题。
摘要由CSDN通过智能技术生成

self.driver.switch_to_alert()做弹窗处理的时候,杯摒弃掉了,但是不影响使用,但是看见报错依旧不舒服,于是寻找一些方法终于解决了这个问题,希望有所帮助
被测网页如下:
在这里插入图片描述
代码如下:
import unittest
from selenium.common.exceptions import NoAlertPresentException
import time
from selenium import webdriver

class MyTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox(executable_path=‘D:\python\driver\geckodriver’)
def test_HandleFrame(self):
url=“file:///D:/alert.html”
#启动自定义网页
self.driver.get(url)
#找到id=button的元素
button=self.driver.find_element_by_id(“button”)
button.click()
try:
#使用driver.switch_to_alert()方法获取alert对象
#alert=self.driver.switch_to_alert()
alert=self.driver.switch_to.alert
time.sleep(2)
#断言文字内容是否是“这是一个alert弹

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值