python3 selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None 错误处理

问题描述:

    使用selenium对网站尝试登陆的时候,因为登陆需要验证码,当输入错误的验证码的时候,网站会显示一个js弹出框,程序会报错。这个报错是我在输入错误的验证码后并尝试打印网页源代码的时候产生的。

弹出框如下:

代码如下:

    # 输入验证码
    username = wait.until(EC.presence_of_all_elements_located(
        (By.XPATH, '//*[@id="code"]')))[0]
    # username.send_keys(verification_code)
    username.send_keys("2345")
    # 登陆
    submit = wait.until(EC.presence_of_all_elements_located(
        (By.XPATH, '//*[@id="sub"]')))[0]
    submit.click()
    time.sleep(10)
    print(browser.page_source)
    time.sleep(35)
    browser.quit()

程序报错如下:

Traceback (most recent call last):
  File "F:/project/全国selenium_测试.py", line 58, in <module>
    huoqu_cookie1 = huoqu_cookie()
  File "F:/project/全国selenium_测试.py", line 50, in huoqu_cookie
    print(browser.page_source)
  File "F:\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 679, in page_source
    return self.execute(Command.GET_PAGE_SOURCE)['value']
  File "F:\project\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "F:\project\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 241, in check_response
    raise exception_class(message, screen, stacktrace, alert_text)
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message: unexpected alert open: {Alert text : 请正确输入验证码!}
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.17134 x86_64)

 原因分析:

    通过报错信息我们可以知道出现弹出框,弹出框为“请正确输入验证码!”,这时网页出现弹出框,右键是没法查看源代码的,只能点击确定。

 

解决方案:

出现弹出框的下面加上如下代码。使用selenium点击确定。

# 判断是否有弹出框
def alert_is_present(driver):
    try:
        alert = driver.switch_to.alert
        alert.text
        return alert
    except:
        return False

# 如果有弹出框 点击确定
if alert_is_present(driver):
    driver.switch_to.alert.accept()


js中三种弹出框:

alert()方法

       alert是这三种对话框中最容易使用的一种,她可以用来简单而明了地将alert()括号内的文本信息显示在对话框中,我们将它称为警示对话框,要显示的信息放置在括号内,该对话框上包含一个“确认”按钮,用户阅读完所显示的信息后,只需单击该按钮就可以关闭对话框。

confirm()方法

      该种对话框上除了包含一个“确认”按钮外,还有一个“取消”按钮,这种对话框称为确认对话框。

prompt()方法
      alert()方法和confirm()方法的使用十分类似,都是仅仅显示已有的信息,但用户不能输入自己的信息,但是prompt()可以做到这点,她不但可以显示信息,而且还提供了一个文本框要求用户使用键盘输入自己的信息,同时她还包含“确认”或“取消”两个按钮
 

selenium中切换到弹框的方法:

browser.switch_to_alert() 

另外:

上面方法在新版本中被废弃,可以继续使用,不过在编辑器中会出现删除线,新版本的selenium中到弹框的方法改为switch_to.alert(),不过在使用新方法后没有按照预想的那样点击网页弹出的弹出框,反而报错,还是继续使用旧版方法即browser.switch_to_alert() ,使用新版方法报错如下:

Traceback (most recent call last):
  File "F:/project/slelenium_弹出框_by_link_text.py", line 26, in <module>
    dr.switch_to.alert().accept()
TypeError: 'Alert' object is not callable

 

selenium中切换到弹框后的四种操作:

1.text 获取弹框文本

2.accept() 确认

3.dimiss() 取消

4.send_keys()  输入字符

例子1:

    # 登陆
    submit = wait.until(EC.presence_of_all_elements_located(
        (By.XPATH, '//*[@id="sub"]')))[0]
    submit.click()
    time.sleep(10)
    browser.switch_to.alert.accept()

例子2:

 
import time
from selenium import webdriver
 
 
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(6)
driver.get("https://www.baidu.com")
time.sleep(1)
 
driver.execute_script("window.alert('这是一个测试Alert弹窗');")
time.sleep(2)
driver.switch_to_alert().accept() 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值