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

编程入门 行业动态 更新时间:2024-10-09 10:25:01
本文介绍了如何在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 seleniummon.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中使用硒检查弹出警报

本文发布于:2023-11-28 01:03:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出   警报   如何在   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!