如何在python中使用硒检查弹出警报

如何在python中使用硒检查弹出警报

本文介绍了如何在python中使用硒检查弹出警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要删除的网页中有弹出消息,我要继续下一次迭代.那就是如果有任何弹出消息,我要接受该消息并转到下一个项目,即转到循环的开头.

What I want is to continue with the next iteration if there is a pop up message in the webpage being scrapped. That is if there is any pop up message, I want to accept that message and go to the next item i.e go to the beginning of the loop.

为此,我使用以下代码段:

For this I use the following snippet of code:

from tkinter import *
from tkinter import messagebox as msg
from tkinter import filedialog as fd
from tkinter import ttk
from tkinter import StringVar as sv
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


for(i in range(0,5)):
      try:
          click_alert=driver.switch_to_alert()
          click_alert.accept()

          continue
      except TimeoutException:
           print('wrong value in'+i+'th row . Please check the value ')

出现以下错误:

Tkinter回调中的异常

Exception in Tkinter callback

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:/Users/chowdhuryr/Desktop/ATT RESEARCH BOT/GUI.py", line 64, in printMessage
    self.Scrapper(str1,str2)
  File "C:/Users/chowdhuryr/Desktop/ATT RESEARCH BOT/GUI.py", line 163, in Scrapper
    click_alert=driver.switchTo().alert()

现在,我非常确定该错误出在click_alert=driver.switch_to_alert()上,因为我已经使用一些健全性检查对其进行了检查.

Now I am pretty certain that the error lies in click_alert=driver.switch_to_alert() because I have checked it using some sanity checks.

推荐答案

通常,我们不会在for后面加上括号.而且,当您打算切换时,警报似乎还不存在. for循环可能只是一个繁忙的等待,使CPU处于循环状态,因此,当警报窗口不存在时,您可以添加一段时间的睡眠,而不仅仅是保持CPU不必要的繁忙.该代码段可以按以下方式更正:

Normally, we don't put parentheses after a for. And, also, it looks like the alert is not present yet when you intend to switch to. Your for-loop could be just a busy wait keeping CPU busy in your loop, so instead of just keeping CPU unnecessarily busy, you may add a sleep for a while when the alert window is not present yet.The code snippet could be corrected as below:

for i in range(0,5):
    try:
        click_alert=driver.switch_to_alert()
        click_alert.accept()
        continue
    except TimeoutException:
        print('wrong value in'+i+'th row . Please check the value ')
    except NoAlertPresentException:
        print('i = ', i, 'alert is not present yet, waiting for some time')
        time.sleep(60) # Delay for 1 minute (60 seconds)
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise

这篇关于如何在python中使用硒检查弹出警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:59